即使使用 -lstdc++fs 也不能 link std::filesystem

Can't link std::filesystem even with -lstdc++fs

我正在尝试编译一个使用 std::filesystem 的应用程序。我使用 CMake 作为构建系统,使用 g++-8 作为编译器。

我的 CMakeLists.txt 包括 target_link_libraries(<target_name> PUBLIC stdc++fs)set(CMAKE_CXX_STANDARD 17)

make VERBOSE=1表示链接器命令中使用了-lstdc++fs

尽管如此,我在使用组件的任何地方都得到了对 std::filesystem 组件的未定义引用。

它在 Docker 中编译良好,所以这显然是一个环境问题。

有什么追踪的技巧吗?

编辑: 由于公司规定,我不能 post 确切的错误消息,而且它也超长。不过,我会 post 一个截断的匿名版本:

CMakeFiles/ProjectName.dir/src/ui/dir_a/file_a.cpp.o: In function `ProjectName::ClassName::update(ProjectName::Body&)':
/home/username/prog/bots/src/ui/dir_a/file_a.cpp:30: undefined reference to `std::filesystem::current_path[abi:cxx11]()'
/home/username/prog/bots/src/ui/dir_a/file_a.cpp:35: undefined reference to `std::filesystem::create_directories(std::filesystem::__cxx11::path const&)'
CMakeFiles/ProjectName.dir/src/ui/dir_a/file_a.cpp.o: In function `std::filesystem::__cxx11::path::operator+=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/include/c++/8/bits/fs_path.h:817: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
CMakeFiles/ProjectName.dir/src/ui/dir_a/file_a.cpp.o: In function `std::filesystem::__cxx11::path::operator+=(char const*)':
/usr/include/c++/8/bits/fs_path.h:825: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
CMakeFiles/ProjectName.dir/src/ui/dir_a/file_a.cpp.o: In function `std::filesystem::exists(std::filesystem::__cxx11::path const&)':
/usr/include/c++/8/bits/fs_ops.h:121: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)'
CMakeFiles/ProjectName.dir/src/ui/dir_b/file_b.cpp.o: In function `ProjectName::ClassName::update[abi:cxx11]()':
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:30: undefined reference to `std::filesystem::current_path[abi:cxx11]()'
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:34: undefined reference to `std::filesystem::create_directories(std::filesystem::__cxx11::path const&)'
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:36: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const'
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:40: undefined reference to `std::filesystem::__cxx11::path::replace_extension(std::filesystem::__cxx11::path const&)'
/home/username/prog/bots/src/ui/dir_b/file_b.cpp:36: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()'
CMakeFiles/ProjectName.dir/src/ui/dir_b/file_b.cpp.o: In function `std::filesystem::__cxx11::path::path(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&, std::filesystem::__cxx11::path::format)':
/usr/include/c++/8/bits/fs_path.h:177: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'

这样持续了一百多行。

软件版本:

Ubuntu 18.04
g++-8 (installed with `apt`)
CMake 3.14.2

升级到 g++ 9 解决了这个问题,但是如果有人知道如何解决这个问题并且仍然使用 g++ 8,我会接受这个答案。