新文件系统库和clang ++ 7的未定义参考错误 [英] Undefined reference error with new filesystem library and clang++7

查看:132
本文介绍了新文件系统库和clang ++ 7的未定义参考错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图推出新的文件系统 STL库,但是由于某种原因出现了错误。 Clang ++ 7 网站表明它应该支持新的文件系统库–我相信 clang 确实比 g ++ 早。

I was trying to out the new filesystem STL library, but for some reason am getting errors. The Clang++7 website indicates that it should support the new filesystem library – indeed clang is running ahead of g++ I believe.

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

I used some code from another Stack Exchange post, so it should be valid based upon the number of upvotes. This could should go to the specified directory and print all files in that directory. Here is the code.

#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)

我确保包括实验/文件系统标头,而不只是 filesystem 标头,该标头删除了Clion中的所有红色花键。我试图从CLion以及从命令行进行编译。我使用的编译字符串是:

I made sure to include the experimental/filesystem header instead of just filesystem which removed any red squiggles in Clion. I tried to compile from CLion as well as from the command line. The compilation string I used was:

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

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

Does anyone have a sense of what is wrong here? In the compile error messages I see the reference to std::experimental::filesystem::v1::__cxx11::.. and I am wondering why this does not say cxx17, but I was not sure if that was the cause of the issue. I explicitly indicated c++17 in the compilation string above.

推荐答案

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

filesystem is still experimental and requires an extra library.

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

If you are using libstdc++, link with -lstdc++fs (or target_link_libraries(${PROJECT_NAME} stdc++fs)).

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

For libc++, use -lc++fs (similar for the CMake command).

这篇关于新文件系统库和clang ++ 7的未定义参考错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆