Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CMAKE_EXPORT_COMPILE_COMMANDS?

I've been trying to use clang-modernize with CMAKE_EXPORT_COMPILE_COMMANDS as recommended in the help of this tool.

With this option cmake generates a JSON file containing compile info like include paths (see also).

This variable is accepted on the command line of cmake, but cmake --help-variable CMAKE_EXPORT_COMPILE_COMMANDS doesn't work (which is coherent with this mailing list posting).

Has someone any idea on how to use it?

I could also use it with cppcheck.

Some more info

I've discovered on a clang developer forum that this cmake feature is not available on all generators. This might change in the future, in the mean time my question remains and I will try too see what happen if I use other generators than Visual Studio.

like image 607
dzada Avatar asked Nov 18 '13 22:11

dzada


People also ask

What is Cmake_export_compile_commands?

CMAKE_EXPORT_COMPILE_COMMANDS. New in version 3.5. Enable/Disable output of compile commands during generation. If enabled, generates a compile_commands.json file containing the exact compiler calls for all translation units of the project in machine-readable form.

How do I run CMake?

Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.

What does Compile_commands json do?

You can use the 'compile_commands. json' file to compile a project or analyze the project by third-party utilities. The PVS-Studio C and C++ analyzer works with this format as well.


2 Answers

I suggest setting

set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 

in the CMakeList.txt

like image 156
Jörn Reimerdes Avatar answered Sep 28 '22 05:09

Jörn Reimerdes


As of CMake 3.5 the CMAKE_EXPORT_COMPILE_COMMANDS option is supported by the Ninja and Makefiles generators.

That means to generate a JSON compile database one has to select a generator that supports it.

For example on UNIX just:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src 

(as it uses the makefile generator there, by default)

Otherwise you can explicitly specify a generator like this:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src -G Ninja 

Or:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src -G 'Unix Makefiles' 

Or another makefiles variant that your cmake supports - a list of supported generators is included in the output of cmake --help.

Note that the compile database JSON file is generated at cmake execution time - not at compile time. Also, with recent clang versions (e.g. clang >= 3.8), clang-modernize was merged into clang-tidy.

like image 22
maxschlepzig Avatar answered Sep 28 '22 03:09

maxschlepzig