代码之家  ›  专栏  ›  技术社区  ›  krishnab

新文件系统库和clang++7的未定义引用错误

  •  2
  • krishnab  · 技术社区  · 6 年前

    我在尝试新的 filesystem STL库,但出于某种原因,我会出错。这个 Clang++7 网站表明它应该支持新的 文件系统 图书馆——事实上 clang 在前面跑 g++ 我相信。

    我使用了来自另一个堆栈交换站的一些代码,因此它应该根据上票的数量有效。这可以转到指定的目录并打印该目录中的所有文件。这是密码。

    #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 标题而不是 文件系统 它消除了阴蒂上的任何红色蠕动。我试着从clion和命令行编译。我使用的编译字符串是:

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

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

    1 回复  |  直到 6 年前
        1
  •  5
  •   krishnab    6 年前

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

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

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