繁体   English   中英

实验性::文件系统链接器错误

[英]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

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

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

您只需要与-lstdc++fs链接即可使用它。

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

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

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

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

link_libraries(stdc++fs)

以便 cmake 可以链接到相应的库。

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

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

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

环境: el6 , gcc/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

额外说明:

以下链接可能会有所帮助

如何使用 devtoolset-8-gcc 安装 gcc8

为了

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 < 11 没有文件系统标头。 macOS < 10.15 在/usr/lib/libc++.1.0.dylib的系统 dylib 中没有std::filesystem::path符号

您可以轻松地在线试用我的代码。

//  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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM