【问题标题】:Can't link std::filesystem even with -lstdc++fs即使使用 -lstdc++fs 也无法链接 std::filesystem
【发布时间】:2019-08-14 13:21:08
【问题描述】:

我正在尝试编译一个使用 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 中编译得很好,所以这显然是一个环境问题。

有什么技巧可以追踪到这一点吗?

编辑: 由于公司规则,我无法发布确切的错误消息,而且它也很长。不过,我会发布一个截断的匿名版本:

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

【问题讨论】:

  • 你用的是什么平台?
  • 能否请您发布确切的错误信息?还有特定版本的软件。你用的是编译器自带的libstdc++还是系统自带的(如果不一样)?
  • @IlyaPopov 感谢您的帮助。我在 OP 中添加了更多细节。我不确定 CMake 链接的是哪个版本的 stdc++,你知道我会如何检查吗?我试试 g++-9。

标签: c++ cmake linker std


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2021-08-12
    相关资源
    最近更新 更多