99

Is there a possibility to debug CMakeLists.txt files (at least listing of variables) except for the message statement?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Peter
  • 9,045
  • 4
  • 39
  • 65

5 Answers5

65

There is no interactive debugger for CMake, however there are also the flags -Wdev, --debug-output and --trace which might help. Also remember to check the log files CMakeFiles\CMakeOutput.log and CMakeFiles\CMakeError.log which mainly collect outputs of processes called by CMake (for example while checking for presence of a type or header).

Since version 3.7, CMake now officially supports a "server mode" so integration in IDEs is likely to improve in the near future. Initial support exists both in Qt Creator and Visual Studio 2017 RC

Joe
  • 6,167
  • 2
  • 28
  • 51
  • Which one do we use to debug `if` statements in `CmakeList.txt`? `came --debug ...` and `cmake --debug-output ...` are not printing them. In Bash, the equivalent is `bash -x `. – jww Sep 14 '16 at 16:44
  • @jww I'm not 100% sure what you are trying to debug, but the output of `--trace` is usually quite extensive (redirect the output to a file!) – Joe Sep 15 '16 at 05:27
  • I was trying to debug the script and understand why certain code blocks were not being entered. But I found the problem: `string(STRIP ...)` was broken. Also see [How to strip trailing newline in Cmake variable?](http://stackoverflow.com/q/39496043/). My apologies if you did not know what `bash -x` does. – jww Sep 15 '16 at 05:44
  • 2
    You shouldn't just mention checking the logs. You should explain how one goes about checking the logs. – Thomas Jay Rush Dec 22 '18 at 13:20
  • @ThomasJayRush I added information about the location and content of the log files. Anything beyond that depends on the specific issue. – Joe Jan 22 '19 at 11:19
12

You can try using the new CMake Script Debugger provided by the VisualGDB tool. It uses an open-source fork of CMake that supports stepping through CMakeLists.txt files, setting code/data breakpoints, evaluating/changing variables, etc.

There's a detailed step-by-step tutorial on the new debugger here

Ivan Shcherbakov
  • 2,023
  • 14
  • 22
7

I like to use variable_watch to "debug" my CMakeLists.txt files. Just set in top of my script:

variable_watch(SOME_MY_VAR)
leanid.chaika
  • 1,803
  • 2
  • 24
  • 27
5

There are steveire's CMake Daemon Tools. I haven't used them myself, but they claim to offer possibilities for introspection that seem to be pretty close to a debugger.

Edit: They are now called CMake-server and are part of CMake 3.7.

usr1234567
  • 19,156
  • 14
  • 105
  • 120
2

Also, read about the env var VERBOSE: https://cmake.org/cmake/help/latest/envvar/VERBOSE.html

I used it this way:

export VERBOSE=defined
make

and got some more verbosity.

In other cases, edit CMakeLists.txt file to include the following line:

set(CMAKE_VERBOSE_MAKEFILE ON)

(Some post on this is https://bytefreaks.net/programming-2/make-building-with-cmake-verbose ).

Also, there are useful cmake options controlling debug output, see the manpage: https://cmake.org/cmake/help/latest/manual/cmake.1.html

Eugene Gr. Philippov
  • 1,589
  • 2
  • 18
  • 18