【发布时间】:2018-06-27 00:05:54
【问题描述】:
我想知道为什么在下面的代码中,没有找到命名空间filesystem:
g++ -std=c++17 main.cpp -lstdc++
// #include <filesystem> <- error, so changed to the following:
#include <experimental/filesystem>
namespace fs = std::filesystem;
int main()
{
return 0;
}
错误:
main.cpp:3:21: error: ‘filesystem’ is not a namespace-name
namespace fs = std::filesystem;
^
main.cpp:3:31: error: expected namespace-name before ‘;’ token
namespace fs = std::filesystem;
gcc 版本 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)
【问题讨论】:
-
如果您的编译器比您尝试使用的标准更旧,那么您可以期望的合规性就只有这么多。
-
std::filesystem对 g++ 的支持从 8.0 版开始 -
另外,为了能够链接,您必须将
-lstdc++fs选项添加到 g++。请参阅en.cppreference.com/w/cpp/filesystem中的注释 -
在
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0中对我来说很好