detecting if libc++ is present

I wanted to automatically set stdlib to libc++ when compiler is clang but I stumbled upon a machine where clang++ was present but

target_link_options(${TARGET_NAME} PUBLIC $<$<LINK_LANGUAGE:CXX>:-stdlib=libc++>) 

emitted an error.
Thus I suppose that libc++ is not necessarily installed along clang++.

What would be the simplest way to test if libc++ can be used ?

Thanks in advance, Regards
A.
Addendum: It seems that certain configurations also require -lc++abi, how can I detect that?

I haven’t tried your case to see if there’s a straightforward way with find_library. However, in general when I need to test something that is easiest to do from a small project, I make a subdirectory and put a simple CMakeLists.txt. I then test that project at CMake configure time using try_compile from the main project.

Example where the fs_check directory is used via try_compile to ensure that c++ standard library filesystem is linkable and actually working. Because the feature check macros don’t always ensure that the standard library works for that particular case.

Thanks, it’s an idea but I’m afraid of the time overhead when reconfiguring the projects. I expected some more straightforward technic.
A.

1 Like