c++ - 新文件系统库和 clang++7 的 undefined reference 错误

标签 c++ c++17

我试图推出新的 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 header ,而不仅仅是 filesystem,它删除了 Clion 中的任何红色波浪形。我尝试从 CLion 和命令行编译。我使用的编译字符串是:

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

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

最佳答案

文件系统 仍处于试验阶段,需要额外的库。

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

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

关于c++ - 新文件系统库和 clang++7 的 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53807011/

相关文章:

c++ - OpenCV drawMatches——queryIdx 和 trainIdx

c++ - .so 中定义的外部变量和可执行文件导致未定义的行为?

c++ - 如何迭代修改后的 std::map 值?

c++ - 不使用 decltype 复制相同的行为

c++ - 在Visual Studio Code中,为什么它会在类之外自动创建 “main::main and main::~main”?

c++ - 数组到指针类模板参数推导适用于模板指南,但不适用于非模板版本,这是正确的还是错误?

c++ - 如何正确异步使用grpc(ClientAsyncReaderWriter)

c++ - 使用 QMenu 并传递参数

c++ - 使用Android NDK在constexpr构造函数(c++ 17)中分配给const char *失败

c++ - 是否可以使用 std::filesystem 获取根名称列表?