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?
Here is a demo that might be helpful to someone in the future:
env:
el6
,gcc/5.5.0
The following are compiling and testing. The flags are
-std=c++17
-lstdc++fs
:It also works with flags:
-std=c++11
The follows had compiling error
_ZNSt12experimental10filesystem2v112current_pathB5cxx11Ev
If you are using cmake, add the following line to
CMakeLists.txt
:So that cmake can link against the corresponding library.
With clang 4.0+, you need to link against
libc++experimental.a
Make sure you're building with libc++ (not libstdc++) with the -stdlib=libc++ (as mentioned in the comments)
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 trunk now also has an implementation of the C++17 Filesystem library, defined in
<filesystem>
and in namespacestd::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.