CMake: Print out all accessible variables in a script

Using the get_cmake_property function, the following loop will print out all CMake variables defined and their values: get_cmake_property(_variableNames VARIABLES) list (SORT _variableNames) foreach (_variableName ${_variableNames}) message(STATUS “${_variableName}=${${_variableName}}”) endforeach() This can also be embedded in a convenience function which can optionally use a regular expression to print only a subset of variables with matching names function(dump_cmake_variables) … Read more

Switching between GCC and Clang/LLVM using CMake

CMake honors the environment variables CC and CXX upon detecting the C and C++ compiler to use: $ export CC=/usr/bin/clang $ export CXX=/usr/bin/clang++ $ cmake .. — The C compiler identification is Clang — The CXX compiler identification is Clang The compiler specific flags can be overridden by putting them into a make override file … Read more

What is the modern method for setting general compile flags in CMake?

For modern CMake (versions 2.8.12 and up) you should use target_compile_options, which uses target properties internally. CMAKE_<LANG>_FLAGS is a global variable and the most error-prone to use. It also does not support generator expressions, which can come in very handy. add_compile_options is based on directory properties, which is fine in some situations, but usually not … Read more

What use is find_package() when you need to specify CMAKE_MODULE_PATH?

Command find_package has two modes: Module mode and Config mode. You are trying to use Module mode when you actually need Config mode. Module mode Find<package>.cmake file located within your project. Something like this: CMakeLists.txt cmake/FindFoo.cmake cmake/FindBoo.cmake CMakeLists.txt content: list(APPEND CMAKE_MODULE_PATH “${CMAKE_CURRENT_LIST_DIR}/cmake”) find_package(Foo REQUIRED) # FOO_INCLUDE_DIR, FOO_LIBRARIES find_package(Boo REQUIRED) # BOO_INCLUDE_DIR, BOO_LIBRARIES include_directories(“${FOO_INCLUDE_DIR}”) include_directories(“${BOO_INCLUDE_DIR}”) add_executable(Bar … Read more

Why is cmake file GLOB evil?

The problem is when you’re not alone working on a project. Let’s say project has developer A and B. A adds a new source file x.c. He doesn’t changes CMakeLists.txt and commits after he’s finished implementing x.c. Now B does a git pull, and since there have been no modifications to the CMakeLists.txt, CMake isn’t … Read more

Passing compiler options cmake

Yes, you can append compiler and linker options. But there are two things you have to differentiate in CMake: first call to generate the build environment and all consecutive calls for re-generating that build environment after changes to your CMakeList.txt files or dependencies. Here are some of the possibilities (excluding the more complex toolchain variants): … Read more

CMake: use a custom linker

The link command line is set in Modules/CMake{C,CXX,Fortran}Information.cmake and defaults to using the compiler, not CMAKE_LINKER (see source code). This can be changed by replacing the rule that builds the link command line, which lives in variables CMAKE_CXX_LINK_EXECUTABLE (and friends). NB that variable does not indicate the path to the linker executable; it says how … Read more