Mapping CMake Variables

Suppose I have a package called Foo. If I run CMake in the CMakeLists.txt file that contains find_package(Foo) , then I can print the values โ€‹โ€‹of variables such as ${Foo_LIBRARIES} and ${Foo_INCLUDES} .

Is there an easy way to display these variables without having to run CMake in the CMakeLists.txt file and without having to manually check the config.cmake file?

+5
source share
4 answers

You asked: (1) Is there an easy way to display these variables without having to run cmake in the CMakeLists.txt file and (2) without having to manually check the config.cmake file?

I can give you the answer "yes" (2), but it requires that you (re) run cmake. But since you can re-run your cmake configuration step by simply doing cmake . in the build directory, re-executing cmake should not stop you from trying this approach. My answer is given in this SO answer and uses the get_cmake_property command. Here is this code encapsulated in the cmake macro, print_all_variables , so I can use it when debugging my cmake scripts.

 macro(print_all_variables) message(STATUS "print_all_variables------------------------------------------{") get_cmake_property(_variableNames VARIABLES) foreach (_variableName ${_variableNames}) message(STATUS "${_variableName}=${${_variableName}}") endforeach() message(STATUS "print_all_variables------------------------------------------}") endmacro() 
+2
source

These variables are usually hardcoded in FindFoo.cmake, so they cannot be extracted without starting the function first. Note that sometimes the value of Foo_LIBRARIES depends on a system configuration that is not known before find_package (Foo) is run.

+1
source

Run CMake and look at the cache using ccmake . Then you will get all the variables.

Or run CMake with -LH. , after which you will get all the variables printed after the configuration.

So, I think it is not possible to get variables without running CMake.

+1
source

Run cmake in search mode. An example of listing include package directories:

 cmake -DNAME=ZLIB -DCOMPILER_ID=GNU -DLANGUAGE=C -DMODE=COMPILE --find-package 

Example library display:

 cmake -DNAME=ZLIB -DCOMPILER_ID=GNU -DLANGUAGE=C -DMODE=LINK --find-package 

The NAME parameter must contain the package name. You can get COMPILER_ID on this page . LANGUAGE can be C , CXX or Fortran .

+1
source

All Articles