GCC和clang的最新版本支持C++17的std::filesystem(
ref
).
为什么这个程序不编译?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::create_directories("sandbox/1/2/a");
fs::create_directory("sandbox/1/2/b");
// fs::permissions("sandbox/1/2/b", fs::perms::remove_perms | fs::perms::others_all);
fs::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
std::system("ls -l sandbox/1/2");
fs::remove_all("sandbox");
}
我用最新的编译器版本测试了它
online compiler
,具有以下编译器选项:
clang++ prog.cc -Wall -Wextra -std=c++17
g++ prog.cc -Wall -Wextra -std=c++2a
从clang我得到了以下链接器错误:
/tmp/prog-9b6259.o: In function `main':
prog.cc:(.text+0x48): undefined reference to `std::__1::__fs::filesystem::__create_directories(std::__1::__fs::filesystem::path const&, std::__1::error_code*)'
prog.cc:(.text+0xad): undefined reference to `std::__1::__fs::filesystem::__create_directory(std::__1::__fs::filesystem::path const&, std::__1::error_code*)'
prog.cc:(.text+0x12f): undefined reference to `std::__1::__fs::filesystem::__create_directory(std::__1::__fs::filesystem::path const&, std::__1::__fs::filesystem::path const&, std::__1::error_code*)'
prog.cc:(.text+0x1b5): undefined reference to `std::__1::__fs::filesystem::__remove_all(std::__1::__fs::filesystem::path const&, std::__1::error_code*)'
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
1
如果我用fs::permissions取消对该行的注释,我会得到以下编译时错误:
prog.cc:11:49: error: no member named 'remove_perms' in 'std::__1::__fs::filesystem::perms'
fs::permissions("sandbox/1/2/b", fs::perms::remove_perms | fs::perms::others_all);
~~~~~~~~~~~^
我从GCC得到了类似的错误。
我做错了什么?这些编译器是否可能支持C++17语言,但不支持其全部库?