find_library does not find stdc++fs

I have the following:

$ find /usr -name '*stdc*'
/usr/lib/gcc/x86_64-redhat-linux/10/32/libstdc++.a
/usr/lib/gcc/x86_64-redhat-linux/10/32/libstdc++.so
/usr/lib/gcc/x86_64-redhat-linux/10/32/libstdc++fs.a
/usr/lib/gcc/x86_64-redhat-linux/10/libstdc++fs.a
/usr/lib/gcc/x86_64-redhat-linux/10/libstdc++.so
/usr/lib64/libstdc++.so.6
/usr/lib64/libstdc++.so.6.0.28

Yet:

find_library(STDCPPFS_LIBRARY NAMES stdc++fs)

tells me “not found”. What can I do ? I cannot hardcode paths as I want that code to work on any Linux distro. I cannot just do a blanket add of target_link_libraries(stdc++fs) because more recent distros / toolchains / … don’t have it, which causes link errors.

I’m not sure why you’d be wanting to find this particular library. I’d expect the compiler would find the right one if you linked to stdc++fs. Either of the following should work:

target_link_libraries(SomeTarget PRIVATE stdc++fs)

To answer your question more directly, find_library() won’t be looking in any of the directories where you have a libstdc++fs.* file. You’d have to provide it with hints, but really I’d be looking to let the compiler provide it on its own with the default linker search path.

You would need to provide the logic that works out whether libstdc++fs.* needs to be linked rather than simply relying on whether you can find such a library or not. That probably means logic that only links it for specific version ranges of specific compilers. For example, cppreference.com contains the following note near the bottom of that page:

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.

I’m not sure why you’d be wanting to find this particular library.

Well, because on some platforms it’s there and on others it’s not and I need my code to work on all these platforms. For instance when I compile with libc++13 under Linux there isn’t any such library as the feature has been merged within libc++ directly.

Shouldn’t cmake guarantee that I can use std::filesystem without issue ?

Also

target_link_libraries(SomeTarget PRIVATE stdc++fs)

does not work, here it tells me that it does not find the library when linking on systems without that lib (linux with a clang/lld toolchain):

ld.lld: error: unable to find library -lstdc++fs

See the part I quoted earlier for clang/LLVM:

You may find CMAKE_CXX_COMPILER_ID and CMAKE_CXX_COMPILER_VERSION useful in working out if and which library you need to link to.

That is when using libc++, I’m still using libstdc++ in some case. (And CMake does not provide an easy way to tell which standard library is being used).

But again: is this an official CMake position that it’s not possible to simply have <filesystem> just work without having to ifdef for every {gcc / clang}x{libc++,libstdc++} version under the sun ?

filesystem is now part of C++17 standard, so it can be used without any special flags on compile or link steps for compilers supporting this standard.

In my opinion, it is too costly (and useless) to try to support, in CMake, compilers offering a non-standard way to use filesystem. Moreover, various compilers, before C++17, offers only a partial or buggy filesystem implementation.