Generating a JSON compilation database
To generate a compile_commands.json file 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 file:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)The compile_commands.json file will be placed in the build directory.
Clang (version 5.0 and later)
Use the -MJ option to write a compilation entry for each input file. You can then merge the outputs into a JSON-formatted compilation database. See an example procedure.
Ninja (version 1.2 and later)
Use the -t compdb option to generate a compilation database. Note that it requires rule names as arguments:
-t compdb rule1 rule2 ...The list of rules can be found in the Ninja build file (typically build.ninja). 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 a single rule named
cc, run:-t compdb cc > compile_commands.jsonTo include multiple rules, extract their names from the build file and pass them to
compdb. See this guide for details.Make-based projects
Use the compiledb-generator tool to create a compilation database for make-based build systems.
Bear and intercept-build tools
Tools like Bear and intercept-build (from scan-build) can generate a compilation database by intercepting compiler calls during the build process.
SourceTrail Visual Studio extension
The SourceTrail Extension generates a compilation database for Visual Studio solutions.