in our project we are using the highest available C and CXX standard by setting
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
However the project is also build with some old compilers that do not support C 20.
Some libs on the other hand require C 20. How can i configure my project so that these libs are only build if the compiler supports C 20?
(I know i could use #ifdef __cplusplus >= ...
but I'm trying to avoid #ifdefs
;))
Thx for your help :)
CodePudding user response:
Well, the variable name speaks for itself. https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD_REQUIRED.html
set(CMAKE_CXX_STANDARD_REQUIRED YES)
You should prefer set_target_properties
.
My expectation is that old compilers automatically skip building the c 20 lib
So do not add target if we don't have C 20.
if(NOT "cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
add_library(the_lib_to_skip ...)
endif()
CodePudding user response:
CMakeLists of libraries should only specify usage requirements.
For example if your library requires at least C 11 standard at build time & consume time:
target_compile_features(mylib PUBLIC cxx_std_11)
With this compile feature, by default CMake takes care to inject proper compilation flags so that this target is compiled with a C standard greater or equal than C 11 (it depends on compiler and its version).
Do not hardcode CMAKE_CXX_STANDARD
in CMakeLists of a library, because it prevents users to compile with a different compatible C standard (and it's not uncommon to want to compile all libraries of a dependency graph with the same C standard).
Then if you want to build this lib with a very specific standard, you can inject CMAKE_CXX_STANDARD
externally during CMake configuration:
cmake -S <source_dir> -B <build_dir> -DCMAKE_CXX_STANDARD=20
=============================================
And now that you have clarified your question:
add_library(mylib1 ...)
# only build lib2 if compiler supports C 20 standard
if("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
add_library(mylib2 ...)
target_compile_features(mylib2 PUBLIC cxx_std_2O)
endif()
I would suggest to display a summary at the end of your configuration, because it can be quite surprising for users to silently disable the build of several libs.