0

最新版本的 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");
}

我使用来自在线编译器的最新编译器版本对其进行了测试,具有以下编译器选项:

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 语言,但不支持它的全部库?

4

0 回答 0