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

2021-12-18 00:00:00 gcc c++ c++17

我尝试在 gcc 6.0 中实际使用新的 c++1z 特性.

如果我试试这个小例子:

#include #include <实验/文件系统>命名空间 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 功能?

解决方案

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

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

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

2017 年 11 月更新:除了文件系统 TS,GCC 8.x 还实现了 C++17 文件系统库,定义在 在使用 -std=gnu++17-std=c++17.GCC 的 C++17 支持尚不完整或稳定,在它被认为可以用于黄金时间之前,您还需要链接到 -lstdc++fs 以获取 C++17 文件系统功能.

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

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

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

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

解决方案

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.

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

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

Update Nov 2017: as well as the Filesystem TS, GCC 8.x also has 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++17 or -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++fs for the C++17 Filesystem features.

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

相关文章