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.


You can also sudo apt install g++-8 and use #include <filesystem> as cppreference described instead of #include <experimental/filesystem> in older g++ and libstdc++ version.

If I install gcc 8 in Ubuntu, will I have 2 different libstdc++ library or merely the original one get updated?

you'll probably have two even though the newer one should work as a drop-in replacement for the old one.

I notice that a libstdc++-8-dev is installed along with g++-8.

This works for me:

g++-8 -g -Wall -std=c++17 test.cpp -lstdc++fs

It seems that even with g++-8, the filesystem library is not automatically linked, you still need to provide -lstdc++fs, and -std=c++17 is also needed in language level.


Following worked for me:

In code:

#include <filesystem>
namespace filesystem = std::filesystem;

In CMakeLists:

set (CMAKE_CXX_FLAGS "-lstdc++fs -std=c++17")

On Ubuntu 18.04 with GCC 10.