82

I'm working on a large C++ project built with CMake on Linux. CMake runs okay, producing a horde of Makefiles in the tree of modules and applications. Running GNU make leads to linker errors. How can I get make to print out the exact commands before running them?

The -d option does not print the commands, but plenty of information that hasn't been helpful.

The -n option prints all the commands, but does not run them, so I can't tell were exactly the trouble is. Examining the stdout from make -n, I don't see any commands that are relevant. I suspect some commands change depending on the results of earlier commands, and the hierarchy of Makefiles makes it difficult to tell what's really going on.

I don't see any other options in make's man page that seem helpful.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DarenW
  • 16,549
  • 7
  • 63
  • 102
  • 4
    possible duplicate of [Using Cmake with GNU Make: How can I see the exact commands?](http://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands) – richq Jan 26 '11 at 20:00

4 Answers4

102

I am fairly sure this will work:

make VERBOSE=1

You should also be able to add this to your CMakeLists.txt to permanently set that:

set(CMAKE_VERBOSE_MAKEFILE on)

This is covered in the CMake FAQ.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • 4
    Yup, that did it! Next thing to debug: why wasn't I able to find that in the documentation? – DarenW Jan 26 '11 at 22:50
  • Is there a way, I can customize the output? For example, if I want to insert custom echo between compiling and linking such as echo "this is the end of compiling and now we will start linking"... – infoclogged Jun 30 '16 at 10:50
  • 1
    I found the answer -> add_custom_command(TARGET yourtargetname POST_BUILD COMMAND echo "WE HAVE FINISHED COMPILING") – infoclogged Jun 30 '16 at 10:59
8

For those using cmake --build, which invokes make internally, use either:

  $ cmake --build <dir> -- VERBOSE=1

Note the -- before VERBOSE=1! That passes the argument to the underlying make process.

Or:

  $ VERBOSE=1 cmake --build <dir>

which also passes VERBOSE=1 to make, this time via an environment variable.

Or, if using cmake version 3.14 or higher:

  $ cmake --build <dir> --verbose

Note the order of the arguments! The --verbose option must come after --build and its argument.

Scott McPeak
  • 8,803
  • 2
  • 40
  • 79
6

For automake-generated Makefiles, try:

make V=1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
k107
  • 15,882
  • 11
  • 61
  • 59
6

An option, which applies to GNU make and works with any Makefile, whether generated by CMake or not, is to use the --trace option to make. This will print out the commands make is executing and still execute them.

This applies to all commands, not just those that VERBOSE=1 or V=1 triggers the printing of in CMake/automake generated makefiles.

And yet another alternative on Linux is to run make under strace, as strace -f -e trace=execve make <make options>. The output from strace will include every process that is executed: by make, by a shell script that make ran, etc.

For instance, you might find that the CMake-generated makefile executes /usr/bin/cmake -E __run_co_compile <lots of options ...> and still wonder what the exact compiler invocation(s) are that this in turn will run. You can get that with the strace method.

TrentP
  • 4,240
  • 24
  • 35