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

问题描述 投票:0回答:6

我尝试在 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
6个回答
213
投票

文件系统 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 开始,可以在没有 std::filesystem 的情况下使用 C++17

-lstdc++fs
组件(但您仍然需要该库来实现
std::experimental::filesystem
)。

2024 年 4 月更新:

从 GCC 13.3 开始,std::experimental::filesystem 符号也可在

-lstdc++exp
中使用(以及其他实验性定义,例如合同违规处理程序和
std::stacktrace
符号)。
    


59
投票
CMakeLists.txt


link_libraries(stdc++fs)

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


14
投票
libc++experimental.a


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


7
投票

环境:

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


1
投票

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,而是适用于从官方安装的 LLVM clang 
https://releases.llvm.org

或通过包管理器安装。 Xcode

std::filesystem::path

系统 dylib 中的符号位于 < 11 doesn't have filesystem header. macOS < 10.15 doesn't have

/usr/lib/libc++.1.0.dylib
    


0
投票
您可以轻松地在线尝试我的代码。

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

© www.soinside.com 2019 - 2024. All rights reserved.