Generating a JSON compilation database
To generate compile_commands.json for your project, you can use compilers, build systems, and specialized tools:
CMake
Use the CMAKE_EXPORT_COMPILE_COMMANDS flag. You can either run
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .or add the following line to your CMakeLists.txt script:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)The compile_commands.json file will be put into the build directory.
Clang (version 5.0 and later)
The -MJ option writes a compilation entry per input file. You can use it for each file in the project, and then merge the outputs into a JSON-formatted compilation database (refer to the procedure example).
Ninja (version 1.2 and later)
To get a compilation database, use the -t compdb option. Note that it requires rule names as arguments:
-t compdb rule1 rule2...
The list of rules is provided in the Ninja build file (default name build.ninja), for example:rule cc command = gcc -c -o $out $in description = CC $out rule link command = gcc -o $out $in description = LINK $outTo generate a compilation database for one rule named
cc
, specify the following:-t compdb cc > compile_commands.jsonBut for multiple rules, you need to get their exact names from the build file and pass them to compdb (refer to one of the possible solutions).
Make-based projects
The compiledb-generator tool creates compilation databases for make-based build systems.
Bear and intercept-build tools
Bear and intercept-build from scan-build are the tools to help you get a compilation database by intercepting compiler calls during the build.
SourceTrail Visual Studio extension
SourceTrail Extension generates a compilation database for Visual Studio solutions.