Enable CMake "compile_commands.json" Output#3854
Merged
paulromano merged 2 commits intoopenmc-dev:developfrom Mar 5, 2026
Merged
Enable CMake "compile_commands.json" Output#3854paulromano merged 2 commits intoopenmc-dev:developfrom
paulromano merged 2 commits intoopenmc-dev:developfrom
Conversation
Enable CMAKE_EXPORT_COMPILE_COMMANDS so that cmake generates a compile_commands.json file in the build directory. This is used by clangd, clang-tidy, and other C++ tooling for accurate code navigation and analysis. No impact on the build itself. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
paulromano
approved these changes
Mar 5, 2026
Contributor
paulromano
left a comment
There was a problem hiding this comment.
I updated this so that it still allows the user to override the setting if they explicitly pass -DCMAKE_EXPORT_COMPILE_COMMANDS=OFF. Otherwise good!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Modern code editors like VSCode with the C++ language tools provide "intellisense" and other linter capabilities to allow for error checking and logical exploration of a codebase. Using these tools often involves VSCode automatically running cmake for the project with the
-DCMAKE_EXPORT_COMPILE_COMMANDS=ONflag. This generates acompile_commands.jsonfile in the build directory that lists exactly which commands are used to compile each file, e.g.,:{ "directory": "/Users/jtramm/openmc/build", "command": "/opt/homebrew/opt/llvm/bin/clang++ -DBUILD_TYPE=RelWithDebInfo -DCOMPILER_ID=Clang -DCOMPILER_VERSION=20.1.2 -DH5_BUILT_AS_DYNAMIC_LIB -DUSE_LIBPNG -Dlibopenmc_EXPORTS -I/Users/jtramm/openmc/include -I/Users/jtramm/openmc/build/include -I/Users/jtramm/openmc/vendor/fmt/include -I/Users/jtramm/openmc/vendor/pugixml/src -isystem /opt/homebrew/include -O2 -g -DNDEBUG -std=c++17 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.5.sdk -fPIC -DH5Oget_info_by_idx_vers=1 -DH5O_info_t_vers=1 -o CMakeFiles/libopenmc.dir/src/bremsstrahlung.cpp.o -c /Users/jtramm/openmc/src/bremsstrahlung.cpp", "file": "/Users/jtramm/openmc/src/bremsstrahlung.cpp", "output": "CMakeFiles/libopenmc.dir/src/bremsstrahlung.cpp.o" }, { "directory": "/Users/jtramm/openmc/build", "command": "/opt/homebrew/opt/llvm/bin/clang++ -DBUILD_TYPE=RelWithDebInfo -DCOMPILER_ID=Clang -DCOMPILER_VERSION=20.1.2 -DH5_BUILT_AS_DYNAMIC_LIB -DUSE_LIBPNG -Dlibopenmc_EXPORTS -I/Users/jtramm/openmc/include -I/Users/jtramm/openmc/build/include -I/Users/jtramm/openmc/vendor/fmt/include -I/Users/jtramm/openmc/vendor/pugixml/src -isystem /opt/homebrew/include -O2 -g -DNDEBUG -std=c++17 -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.5.sdk -fPIC -DH5Oget_info_by_idx_vers=1 -DH5O_info_t_vers=1 -o CMakeFiles/libopenmc.dir/src/cell.cpp.o -c /Users/jtramm/openmc/src/cell.cpp", "file": "/Users/jtramm/openmc/src/cell.cpp", "output": "CMakeFiles/libopenmc.dir/src/cell.cpp.o" },In turn, many of these intellisense and linter tools will then read this json file so that they can know e.g. exactly which headers to follow, which macros are defined, etc.
OpenMC's CMake doesn't currently run this flag so the editor needs to re-run cmake manually. Not a problem for VSCode, but I'm making a skill for Claude code that uses
clangd(basically the same idea as VSCode intellisense) to improve the agent's ability to navigate OpenMC. This requires that acompile_commands.jsonis present. If it's not there, it makes it a little more complex for the agent as it has to go back and figure out how to compile OpenMC etc. As such, it seems like it doesn't hurt anything to add this flag into CMake so that it's there consistently. The resulting file on my system is only 177 kB, and just sits in the build folder, so doesn't seem objectionable. The build isn't otherwise modified -- you just get the addedcompile_commands.jsonfile output.Checklist