CMake Ninja show link commands
CMake can show the compile commands that will be issued to compile targets by setting the CMAKE_EXPORT_COMPILE_COMMANDS variable to ON.
This generates ${CMAKE_BINARY_DIR}/compile_commands.json file that contains the
JSON compilation database
for each source file.
However, this does not include the link commands used to link the final executable or library.
There are a couple ways to make the build system emit the link commands without actually yet linking the target:
Ninja JSON compilation database method
To show the compile and link commands in JSON compilation database format using Ninja for a specific target including target dependencies use the compdb-targets Ninja tool.
cmake -B build -G Ninja
cmake --build build -- -t compdb-targets MyTarget > compile_and_link_commands.json
# equivalent to
ninja -C build -t compdb-targets MyTarget > compile_and_link_commands.jsonGNU Make or Ninja dry-run method
The Ninja compdb-targets method above is preferred, since the drawbacks of this “dry-run” method include:
- have to clean-first to show all the dependecies, otherwise if some dependencies have been built already, they won’t be shown in the output.
- the output is not in JSON format, so it may be harder to parse.
The -n dryrun flag shows the compile and link commands without executing them.
cmake -B build
cmake --build build --target mytarg --verbose --clean-first -- -n