350

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?

usr1234567
  • 21,601
  • 16
  • 108
  • 128
Nils
  • 13,319
  • 19
  • 86
  • 108

8 Answers8

470

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
Zoe
  • 27,060
  • 21
  • 118
  • 148
richq
  • 55,548
  • 20
  • 150
  • 144
  • 13
    These solutions make it too verbose, printing a lot of CMake and make internals that are not interesting. Is there any way to only show compile and link commands (i.e. what is usually relevant for debugging problems). Perhaps even only display the command that failed. – Tronic Feb 01 '13 at 17:58
  • The top google hit recommends ["if you want to see solely the g++ command lines, you should use grep & Co. - if possible - in connection with verbose Makefiles"](https://cmake.org/pipermail/cmake/2011-May/044451.html). Perhaps developers with lots of cmake experience have other advice. – Trevor Boyd Smith Oct 07 '15 at 12:26
  • 12
    By the way, if you are on a platform that supports it `cmake -GNinja . ; ninja -v` shows a very nice verbose output with minimal fluff. – richq Oct 07 '15 at 12:55
  • 19
    Single line... `cmake --build . -- VERBOSE=1` – letmaik Feb 20 '17 at 12:10
  • Apparently, I must be working with an newer cmake version, because there are no makefiles found in my build directory after running cmake... – jaques-sam May 27 '19 at 13:09
  • Ok found it: need to run cmake with generate "Unix Makefiles": `cmake .. -G"Unix Makefiles"` – jaques-sam May 27 '19 at 13:25
  • second time here, I'd like to vote up for this answer ones more ) – kyb May 29 '19 at 10:06
  • 3
    One can also use `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON` in that case, such that we have all the commands in the file `compile_commands.json` without actually building the sources. – fgiraldeau Sep 01 '20 at 14:48
  • `-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON` works for me at `Clion`, cmake: 3.20+ – FavorMylikes Sep 23 '21 at 10:31
102

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

set(CMAKE_VERBOSE_MAKEFILE ON)
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
James Hirschorn
  • 7,032
  • 5
  • 45
  • 53
  • 8
    Please don't do this for your own projects. Not all your users may want to see your verbose build output. Don't force this on everybody consuming your library. `CMAKE_VERBOSE_MAKEFILE ` should be off by default and a user's choice, if they need it, for debugging purposes. – Ela782 Mar 11 '18 at 19:07
  • 4
    So? You can always add it as a cache variable if you have multiple developers and want to give them a choice. But yes, in a release version of a project you should have it off by default. – James Hirschorn Mar 24 '18 at 04:04
  • 1
    This is a great option when using qt.io QtCreator because cmake is called by the GUI. Thank you! – Grant Rostig Feb 11 '19 at 21:56
16

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

makerj
  • 2,179
  • 2
  • 18
  • 27
  • 1
    On Windows, setting environment variable `VERBOSE` to `1` works both for MSVC & MinGW. If you want to undo this, you need to set it to empty, not `0`. – kbridge4096 Jan 02 '21 at 09:42
11

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.

Tested on Cmake 3.22.1, Ubuntu 22.04.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
8

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

SteveL
  • 1,811
  • 1
  • 13
  • 23
5

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.

3

CMake 3.14+

CMake now has --verbose to specify verbose build output. This works regardless of your generator.

cd project
cmake -B build/
cmake --build build --verbose

It's worth noting however Xcode may not work with --verbose

Some generators such as Xcode don't support this option currently.

Another option it to use the VERBOSE environment variable.

New in version 3.14.

Activates verbose output from CMake and your build tools of choice when you start to actually build your project.

Note that any given value is ignored. It's just checked for existence.
jpr42
  • 718
  • 3
  • 14
0

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.