88

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?

starball
  • 20,030
  • 7
  • 43
  • 238
Karnivaurus
  • 22,823
  • 57
  • 147
  • 247

6 Answers6

70

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()
user3325146
  • 207
  • 2
  • 12
Phil
  • 5,822
  • 2
  • 31
  • 60
70

To simply print a value, you could do something like this:

message(STATUS "foo include dir: ${foo_INCLUDE}")

where ${foo_INCLUDE} is the value you desire to print.

Note: I'm using cmake > 3.14

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Aaron B.
  • 1,605
  • 13
  • 12
6

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.

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

usr1234567
  • 21,601
  • 16
  • 108
  • 128
4

Run cmake in find-package mode. Example to display a package`s include directories:

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

Example to display the libraries:

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

The NAME must be set to the package name. You can obtain your COMPILER_ID on this page. LANGUAGE can be C, CXX or Fortran.

tamas.kenez
  • 7,301
  • 4
  • 24
  • 34
  • 1
    I like this idea, but it doesn't seem to work for me:`$ uname -a Linux kaveri 4.18.0-16-generic #17-Ubuntu SMP Fri Feb 8 00:06:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux $ ls /usr/lib/x86_64-linux-gnu/cmake/assimp-4.1/ assimp-config.cmake assimp-config-version.cmake $ cmake -DNAME=assimp-4.1 -DCOMPILER_ID=GNU -DLANGUAGE=CXX -DMODE=LINK --find-package assimp-4.1 not found. CMake Error: Problem processing arguments. Aborting.` – user2023370 Mar 28 '19 at 10:40
  • The package name is `assimp`, try with `-DNAME=assimp` – tamas.kenez Mar 29 '19 at 21:02
  • I still get the same response: assimp not found etc. – user2023370 Mar 29 '19 at 21:18
2

I am always suspicious of variables changing values throughout a script somewhere so I like to see the value of a variable at a particular point in the running script. Combining the ideas from both Phil and Aaron B. this is what I'm using:

function(PRINT_VAR VARNAME)
  message(STATUS "${VARNAME}: ${${VARNAME}}")
endfunction()
PRINT_VAR("CMAKE_CXX_COMPILER")

Then I can just litter PRINT_VAR statements around like I'm debugging code back in 1980

Vince W.
  • 3,561
  • 3
  • 31
  • 59
1

These variables are generally hardcoded into FindFoo.cmake so that it is not possible to extract them without running the function first. Note that sometimes the value of Foo_LIBRARIES depends on the system configuration, which is unknown until find_package(Foo) is run.

Raul Laasner
  • 1,475
  • 1
  • 17
  • 30