experimental::filesystem 链接器错误

experimental::filesystem linker error

我尝试在 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: In function \`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: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status

gcc 版本是快照linux-gnu_6-20151011

关于如何 link 新的 c++1z 特性的任何提示?

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

您只需 link 和 -lstdc++fs 即可使用它。

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

2017 年 11 月更新: 以及文件系统 TS,GCC 8.x 有 C+ 的实现+17 文件系统库,在使用 -std=gnu++17-std=c++17 时在 <filesystem> 和命名空间 std::filesystem 中定义(N.B。这些名称中没有“实验性”)。 GCC 的 C++17 支持还不完整或稳定,在它被认为可以使用之前,您还需要 link 到 -lstdc++fs 以获得 C++17 文件系统功能。

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

的图书馆

使用 clang 4.0+,您需要 link 反对 libc++experimental.a

确保您使用 libc++(而不是 libstdc++)和 -stdlib=libc++(如评论中所述)进行构建

这是一个可能对以后的人有帮助的演示:

环境: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

补充说明:

以下link可能会有帮助

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

link_libraries(stdc++fs)

这样cmake就可以link针对相应的库

为了

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,link 和 libc++.1.0.dylib 由 LLVM 提供。

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 < 11 没有文件系统 header。 macOS < 10.15 在 /usr/lib/libc++.1.0.dylib

的系统动态库中没有 std::filesystem::path 符号

You can easily try my code online.

//  currentPath.cpp
// 
#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"