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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33149878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:13:06  来源:igfitidea点击:

experimental::filesystem linker error

c++gccc++17

提问by Klaus

I try to use the new c++1z features actually on the head of development within gcc 6.0.

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

If I try this little example:

如果我尝试这个小例子:

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

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

I got:

我有:

/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 version is the snapshot linux-gnu_6-20151011

gcc 版本是快照 linux-gnu_6-20151011

Any hints how to link for the new c++1z features?

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

回答by Jonathan Wakely

The Filesystem TS is nothing to do with C++1z support, it is a completely separate specification not part of the C++1z working draft. GCC's implementation (in GCC 5.3 and later) is even available in C++11 mode.

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

You just need to link with -lstdc++fsto use it.

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

(The relevant library, libstdc++fs.a, is a static library, so as with any static library it should come afterany objects that depend on it in the linker command.)

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

Update Nov 2017:as well as the Filesystem TS, GCC trunk now alsohas an implementation of the C++17 Filesystem library, defined in <filesystem>and in namespace std::filesystem(N.B. no "experimental" in those names) when using -std=gnu++17or -std=c++17. GCC's C++17 support is not complete or stable yet, and until it's considered ready for prime time use you also need to link to -lstdc++fsfor the C++17 Filesystem features.

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

Update Jan 2019:starting with GCC 9, the C++17 std::filesystemcomponents can be used without -lstdc++fs(but you still need that library for std::experimental::filesystem).

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

回答by Searene

If you are using cmake, add the following line to CMakeLists.txt:

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

link_libraries(stdc++fs)

So that cmake can link against the corresponding library.

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

回答by xaxxon

With clang 4.0+, you need to link against libc++experimental.a

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

Make sure you're building with libc++ (not libstdc++) with the -stdlib=libc++ (as mentioned in the comments)

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

回答by caot

Here is a demo that might be helpful to someone in the future:

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

env: el6, gcc/5.5.0

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

The following are compiling and testing. The flags are -std=c++17-lstdc++fs:

以下是编译和测试。标志是-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

It also works with flags: -std=c++11

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

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

The follows had compiling error _ZNSt12experimental10filesystem2v112current_pathB5cxx11Ev

以下有编译错误 _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