【问题标题】:experimental::filesystem linker error实验性::文件系统链接器错误
【发布时间】:2016-01-13 23:46:26
【问题描述】:

我尝试在 gcc 6.0 中实际使用新的 c++1z 功能。

如果我尝试这个小例子:

#include <iostream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::path p1 = "/home/pete/checkit";

    std::cout << "p1 = " << p1 << std::endl;
}

我明白了:

/opt/linux-gnu_6-20151011/bin/g++ --std=c++1z main.cpp -O2 -g -o go /tmp/ccaGzqFO.o:在函数\`std::experimental::filesystem::v1::__cxx11::path::path(char const (&) [36])'中: /opt/linux-gnu_6-20151011/include/c++/6.0.0/experimental/bits/fs_path.h:167: 未定义引用`std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts ()' collect2:错误:ld 返回 1 个退出状态

gcc 版本是快照 linux-gnu_6-20151011

任何提示如何链接新的 c++1z 功能?

【问题讨论】:

    标签: c++ gcc c++17


    【解决方案1】:

    文件系统 TS 与 C++1z 支持无关,它是一个完全独立的规范,不是 C++1z 工作草案的一部分。 GCC 的实现(在 GCC 5.3 及更高版本中)甚至可以在 C++11 模式下使用。

    你只需要链接-lstdc++fs就可以使用它。

    (相关库libstdc++fs.a 是一个静态库,因此与任何静态库一样,它应该位于链接器命令中依赖它的任何对象之后。)

    2017 年 11 月更新: 除了文件系统 TS,GCC 8.x 具有 C++17 文件系统库的实现,在 &lt;filesystem&gt; 中定义当使用-std=gnu++17-std=c++17 时,在命名空间std::filesystem 中(注意这些名称中没有“实验性”)。 GCC 的 C++17 支持还不完整或不稳定,在它被认为可以在黄金时段使用之前,您还需要链接到 -lstdc++fs 以获取 C++17 文件系统功能。

    2019 年 1 月更新:从 GCC 9 开始,C++17 std::filesystem 组件可以在没有 -lstdc++fs 的情况下使用(但您仍然需要 std::experimental::filesystem 的库)。

    【讨论】:

    • 这是否记录在任何地方,我试图自己确定这一点,但一无所获,我在这里错过了一些资源吗?
    • 当我尝试使用它时,我得到相同的链接器错误。 c++ -lstd++fs main.cpp。我正在使用gcc version 5.3.1 20151207 (Red Hat 5.3.1-2) (GCC)
    • 好的,-lstdc++fs 必须在行尾(至少在源文件之后)。我不明白为什么有些-lxxx 需要放在最后,而另一些不需要。
    • @alfC 因为这就是链接器的工作方式。引用是从左到右解析的,因此您需要在使用它们的对象之后列出静态库。
    【解决方案2】:

    如果您使用的是 cmake,则将以下行添加到 CMakeLists.txt:

    link_libraries(stdc++fs)
    

    这样cmake就可以链接对应的库了。

    【讨论】:

    • 我做了target_link_libraries(hello_world_ stdc++fs) 并编译了。
    • target_link_librariesg++-8 中对我不起作用。这有效
    【解决方案3】:

    使用 clang 4.0+,您需要链接到 libc++experimental.a

    确保使用 -stdlib=libc++(如 cmets 中所述)使用 libc++(不是 libstdc++)进行构建

    【讨论】:

    • 我还需要 -stdlib=libc++,因为我的 clang 版本意外使用了 libstdc++。
    • @BowieOwens 谢谢,更新答案以明确这一点。
    • 当您说“确保您使用 libc++ 构建”时,我该怎么做? (最好用 CMake 解决。)。谢谢。
    • @mannygover -stdlib=libc++set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
    【解决方案4】:

    这是一个演示,将来可能对某人有所帮助:

    环境:el6gcc/5.5.0

    #include <iostream>
    #include <string>
    #include <experimental/filesystem>
    
    int main()
    {
        std::string path = std::experimental::filesystem::current_path();
    
        std::cout << "path = " << path << std::endl;
    }
    

    以下是编译和测试。标志是-std=c++17-lstdc++fs

    $ g++ -v
    Using built-in specs.
    COLLECT_GCC=g++
    COLLECT_LTO_WRAPPER=/apps/gcc-5.5.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/5.5.0/lto-wrapper
    Target: x86_64-unknown-linux-gnu
    Configured with: ../configure --prefix=/apps/gcc-5.5.0 --disable-multilib --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=all
    Thread model: posix
    gcc version 5.5.0 (GCC)
    
    $ ls -lrt /apps/gcc-5.5.0/lib64 | grep libstdc
    -rwxr-xr-x. 1 root root  11272436 Jun 25 10:51 libstdc++.so.6.0.21
    -rw-r--r--. 1 root root      2419 Jun 25 10:51 libstdc++.so.6.0.21-gdb.py
    -rwxr-xr-x. 1 root root       976 Jun 25 10:51 libstdc++.la
    -rwxr-xr-x. 1 root root  11272436 Jun 25 10:51 libstdc++.so
    -rw-r--r--. 1 root root  10581732 Jun 25 10:51 libstdc++fs.a
    -rw-r--r--. 1 root root  28985412 Jun 25 10:51 libstdc++.a
    -rwxr-xr-x. 1 root root       916 Jun 25 10:51 libstdc++fs.la
    -rwxr-xr-x. 1 root root  11272436 Jun 25 10:51 libstdc++.so.6
    
    $ g++ filesystem-testing.cpp -lstdc++fs -std=c++17
    $ ./a.out
    
    $ g++ -std=c++17 filesystem-testing.cpp -lstdc++fs
    $ ./a.out
    path = /home/userid/projects-c++/filesystem-testing
    

    它也适用于标志:-std=c++11

    $ g++ -std=c++11 filesystem-testing.cpp -lstdc++fs
    $ ./a.out
    path = /home/userid/projects-c++/filesystem-testing
    

    以下有编译错误_ZNSt12experimental10filesystem2v112current_pathB5cxx11Ev

    $ g++ -std=c++17 -lstdc++fs filesystem-testing.cpp
    /tmp/ccA6Q9oF.o: In function `main':
    filesystem-testing.cpp:(.text+0x11): undefined reference to `_ZNSt12experimental10filesystem2v112current_pathB5cxx11Ev'
    collect2: error: ld returned 1 exit status
    

    补充说明:

    以下链接可能会有所帮助

    How to install gcc8 using devtoolset-8-gcc

    【讨论】:

      【解决方案5】:

      对于

      dyld: lazy symbol binding failed: Symbol not found: 
      __ZNSt3__14__fs10filesystem4path17replace_extensionERKS2_
      

      Undefined symbols for architecture x86_64:   
      "std::__1::__fs::filesystem::__current_path(std::__1::error_code*)", 
      referenced from:      
      sys::path::get_cwd() in Path.cc.o
      ld: symbol(s) not found for architecture x86_64
      

      .. 尝试以下方法:

      对于 LLVM clang >= 10,使用 LLVM 提供的 libc++.1.0.dylib 链接。

      add_link_options("-Wl,-rpath,location_of_llvm_install/lib;location_of_llvm_install/lib/libc++.1.0.dylib")
      

      这不是针对 Apple Clang,而是针对从官方 https://releases.llvm.org 或包管理器安装的 LLVM clang。

      Xcode /usr/lib/libc++.1.0.dylib 的系统dylib 中没有std::filesystem::path 符号

      【讨论】:

        【解决方案6】:

        You can easily try my code online.

        //  currentPath.cpp
        // https://stackoverflow.com/questions/33149878#65128193
        #include <experimental/filesystem>
        #include <iostream>
        using namespace std;
        
        int main() {
          cout << "path = " << experimental::filesystem::current_path() << endl;
        }
        

        编译运行:

        clang++ currentPath.cpp -lstdc++fs && ./a.out # Linux
        clang++ currentPath.cpp -lstdc++fs && ./a.exe # MSYS2, Windows
        

        注意:-lstdc++fs 是链接器标志,而不是编译器标志。 (在编写 makefile 时很重要。)

        预期输出:

        path = "/path/to/the/current/directory"
        

        【讨论】:

        • 如果您尝试编译并运行我的代码,我相信您会看到一个很好的理由来支持我的答案。如果您确实编译并运行,但仍然不想投票,请考虑在下面的评论中告诉我为什么。 - 诚然,我的回答与@caot 的回答确实有相似之处,但也存在一些显着差异。这就是为什么我更愿意发布自己的答案,而不是建议对 caot 的答案进行修改。
        猜你喜欢
        • 1970-01-01
        • 2015-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多