c++gccstd-filesystem

Linking to stdc++fs with C++20 still necessary when using filesystem?


I began using experimental::filesystem when it had just come out and I was advised to link in stdc++fs using target_link_libraries(MyTarget stdc++fs) in CMake. My programme builds either way but may I be breaking something/ doing something unnecessary if I leave out the extra link command?

This question here was asked three years ago and seems to assume that linking in stdc++fs was necessary.

Edit: compiler version is g++-9 (Homebrew GCC 9.3.0_1) 9.3.0 and CMake version is 3.16.

Many thanks!


Solution

  • Lifted from my comment:

    I guess it depends on your compiler version. From cppreference, At the very bottom of the File System Library page, you can find:

    Using this library may require additional compiler/linker options. GNU implementation prior to 9.1 requires linking with -lstdc++fs and LLVM implementation prior to LLVM 9.0 requires linking with -lc++fs

    As you mentioned, the following link has more details about how to check compiler identity in CMake: How can I add a minimum compiler version requisite?