Using CMake with GNU Make: How can I see the exact commands?

CmakeGnu Make

Cmake Problem Overview


I use CMake with GNU Make and would like to see all commands exactly (for example how the compiler is executed, all the flags etc.).

GNU make has --debug, but it does not seem to be that helpful are there any other options? Does CMake provide additional flags in the generated Makefile for debugging purpose?

Cmake Solutions


Solution 1 - Cmake

When you run make, add VERBOSE=1 to see the full command output. For example:

cmake .
make VERBOSE=1

Or you can add -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON to the cmake command for permanent verbose command output from the generated Makefiles.

cmake -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON .
make

To reduce some possibly less-interesting output you might like to use the following options. The option CMAKE_RULE_MESSAGES=OFF removes lines like [ 33%] Building C object..., while --no-print-directory tells make to not print out the current directory filtering out lines like make[1]: Entering directory and make[1]: Leaving directory.

cmake -DCMAKE_RULE_MESSAGES:BOOL=OFF -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON .
make --no-print-directory

Solution 2 - Cmake

It is convenient to set the option in the CMakeLists.txt file as:

set(CMAKE_VERBOSE_MAKEFILE ON)

Solution 3 - Cmake

Or simply export VERBOSE environment variable on the shell like this:

export VERBOSE=1

Solution 4 - Cmake

If you use the CMake GUI then swap to the advanced view and then the option is called CMAKE_VERBOSE_MAKEFILE.

Solution 5 - Cmake

I was trying something similar to ensure the -ggdb flag was present.

Call make in a clean directory and grep the flag you are looking for. Looking for debug rather than ggdb I would just write.

make VERBOSE=1 | grep debug

The -ggdb flag was obscure enough that only the compile commands popped up.

Solution 6 - Cmake

cmake --build . --verbose

On Linux and with Makefile generation, this is likely just calling make VERBOSE=1 under the hood, but cmake --build can be more portable for your build system, e.g. working across OSes or if you decide to do e.g. Ninja builds later on:

mkdir build
cd build
cmake ..
cmake --build . --verbose

Its documentation also suggests that it is equivalent to VERBOSE=1:

> --verbose, -v > > Enable verbose output - if supported - including the build commands to be executed. > > This option can be omitted if VERBOSE environment variable or CMAKE_VERBOSE_MAKEFILE cached variable is set.

Solution 7 - Cmake

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE will generate a file with all compilation commands.

This file is required by some LSP to know how to compile a source file out of the box, but it could also help for debugging compilation problems.

The output file is named ${CMAKE_BINARY_DIR}/compile_commands.json.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNilsView Question on Stackoverflow
Solution 1 - CmakerichqView Answer on Stackoverflow
Solution 2 - CmakeJames HirschornView Answer on Stackoverflow
Solution 3 - CmakemakerjView Answer on Stackoverflow
Solution 4 - CmakeSteveLView Answer on Stackoverflow
Solution 5 - CmakeAlexander GriffithView Answer on Stackoverflow
Solution 6 - CmakeCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 7 - CmakeStéphane ClérambaultView Answer on Stackoverflow