Link errors using filesystem members in C++17
Add the flag -lstdc++fs
:
$ g++-7 test.cpp -std=c++17 -lstdc++fs
gcc 7.2 supports C++17 experimental filesystem
namespace only. I do not know, maybe gcc 7.3 supports std filesystem
namespace already.
experimental::filesystem linker error
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
).
std::filesystem::directory_iterator linker issue (C++17)
Per this reddit post by u/forcecharlie you need to add -lstdc++fs
to the compiler options. On Wandbox if we don't add it we get link errors but when we do add it, it compiles successfully.
c++17 `filesystem` is not a namespace-name
GCC 5.4.0 was released in June of 2016; over a year before the C++17 standard was adopted. It and its version of libstdc++ have very limited C++17 support. You can see when GCC added C++17 language features here and when libstdc++ added C++17 standard library features here.
At the time of GCC 5.4's release, the filesystem library was not yet implemented in the std::filesystem
namespace. It, along with any other <experimental/...> headers that are included in that version, are in the std::experimental
namespace.
std::filesystem example not compiling on c++ 17
There was a defect in C++17 standard that didn't allow operator<<
to be called with std::filesystem::directory_entry
, reported in LWG 3171. It's now fixed as defect report, but it seems clang only fixed it in version 14: https://godbolt.org/z/3arTcGYvY. gcc seems to have backported the fix to all versions that support std::filesystem
(that is, gcc9.1 and up): https://godbolt.org/z/fh7cdMxso
Linking to std++fs - what syntax to choose
Previously, the filesystem part of the standard library wasn't included in libstdc++
but was only available by explicitly linking with libstdc++fs
. g++
is still shipped with libstdc++fs
so explicitly linking with it isn't a problem even for newer g++
versions.
I was hoping to find a clean cmake way of doing find_package
similar to that of when using threads
find_package(Threads REQUIRED)
target_link_libraries(application PRIVATE Threads::Threads)
but unfortunately, I only found how to do that for Boost::filesystem
...
Related Topics
Matlab VS C++ Double Precision
C++ Equivalent of Stringbuffer/Stringbuilder
How to Copy Contents of a Directory into Build Directory After Make with Cmake
Can Openssl on Windows Use the System Certificate Store
The Implementation of Std::Forward
Operator= and Functions That Are Not Inherited in C++
When Is It Worthwhile to Use Bit Fields
Std::Put_Time Implementation Status in Gcc
Non-Defaulted Operator <=> Doesn't Generate == and != in C++20
How to Make an Array with a Dynamic Size? General Usage of Dynamic Arrays (Maybe Pointers Too)
How to Rearrange a String Equation
How to Append a Char to a Std::String
How to Generate Thread-Safe Uniform Random Numbers
Disambiguate Overloaded Member Function Pointer Being Passed as Template Parameter