我尝试在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。0:在函数 ' std::experimental::filesystem::v1::__cxx11::path::path(char const(&)[36])'中:/opt/linux- gnu6 -20151011/include/c++/6.0.0/experimental/bits/fs_path.h:167:对' std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'的未定义引用Collect2: error: ld returned 1 exit status之前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文件系统库的实现,在<filesystem>
和命名空间std::filesystem
中定义(注意:no "experimental";当使用-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)
使用clang 4.0+,您需要链接到libc++experimental.a
使用-stdlib=libc++(如注释中所述)
下面是一个可能对将来有帮助的演示:
env: 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
标题>For
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 & lt;11没有文件系统头。macOS & lt;10.15在/usr/lib/libc++.1.0.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"