【问题标题】:Undefined reference error with new filesystem library and clang++7新文件系统库和 clang++7 的未定义引用错误
【发布时间】:2018-12-16 22:27:35
【问题描述】:

我试图推出新的filesystem STL 库,但由于某种原因出现错误。 Clang++7 网站表明它应该支持新的filesystem 库——我相信clang 确实领先于g++

我使用了另一个 Stack Exchange 帖子中的一些代码,因此它应该是有效的,基于赞成票的数量。这应该转到指定的目录并打印该目录中的所有文件。这是代码。

#include <iostream>
#include <string>
#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

int main(int argc, char *argv[])
{

    std::string path = "/home/.../Downloads";
    for (const auto & entry : fs::directory_iterator(path))
    {
        std::cout << entry.path() << std::endl;
    }

}

我收到的错误消息是:

CMakeFiles/filesystem_app.dir/main.cpp.o: In function `main':
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator*() const'
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator++()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `path<std::__cxx11::basic_string<char>, std::experimental::filesystem::v1::__cxx11::path>':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_path.h:198: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `directory_iterator':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_dir.h:188: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::directory_iterator(std::experimental::filesystem::v1::__cxx11::path const&, std::experimental::filesystem::v1::directory_options, std::error_code*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我确保包含 experimental/filesystem 标头,而不仅仅是 filesystem,它删除了 Clion 中的所有红色曲线。我尝试从 CLion 以及命令行进行编译。我使用的编译字符串是:

  clang++-7 -Wall -std=c++17 main.cpp -o app

有人知道这里出了什么问题吗?在编译错误消息中,我看到了对std::experimental::filesystem::v1::__cxx11::.. 的引用,我想知道为什么这没有说cxx17,但我不确定这是否是问题的原因。我在上面的编译字符串中明确指出了c++17

【问题讨论】:

标签: c++ c++17


【解决方案1】:

filesystem 仍处于试验阶段,需要额外的库。

如果您使用的是 libstdc++,请使用-lstdc++fs(或target_link_libraries(${PROJECT_NAME} stdc++fs))链接。

对于 libc++,使用 -lc++fs(与 CMake 命令类似)。

【讨论】:

  • 哈哈,明白了。是的,我没有看到有人提到这个-lstdc++fs 标志。非常感谢您在这方面的帮助。
  • 是的,这很不幸。如果我理解正确,由于不久的将来可能发生 ABI 变化,所有编译器都是一样的。我可能错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
  • 2017-05-02
相关资源
最近更新 更多