Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying CMake variables

Tags:

cmake

Suppose I have a package called Foo. If I run CMake on a CMakeLists.txt file that contains find_package(Foo), then I can print out 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 on a CMakeLists.txt file, and without having to manually inspect the config.cmake file?

like image 795
Karnivaurus Avatar asked Jul 10 '15 14:07

Karnivaurus


People also ask

How do I view CMake variables?

Another way to view all cmake's internal variables, is by executing cmake with the --trace-expand option. This will give you a trace of all . cmake files executed and variables set on each line.

How do I print a variable in CMake?

Run CMake and have a look at the cache with the ccmake GUI tool. Then you'll get all the variables. Or run CMake with -LH then you will get all variables printed after configuration.

How do I use CMake variables?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake . You can unset entries in the Cache with unset(... CACHE) .

What is ${} in CMake?

Local Variables You access a variable by using ${} , such as ${MY_VARIABLE} . 1. CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.


1 Answers

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

I can give you a yes answer to (2) but it does require that you (re)run cmake. But since you can re-run your cmake configure step by simply executing cmake . in the build directory, re-running cmake should not keep you from trying this approach. My answer is given in this SO answer and uses the get_cmake_property command. Here is that code encapsulated into a 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() 

The macros are invoked with same syntax as cmake functions:

print_all_variables() 
like image 97
Phil Avatar answered Sep 19 '22 22:09

Phil