Skip to content

Instantly share code, notes, and snippets.

@ungive
Created April 4, 2026 13:37
Show Gist options
  • Select an option

  • Save ungive/988b0e4d8d582dd687d69eba0ce6737a to your computer and use it in GitHub Desktop.

Select an option

Save ungive/988b0e4d8d582dd687d69eba0ce6737a to your computer and use it in GitHub Desktop.
Generate compile_commands.json automatically for clangd with CMake
# Automatically create and update a symlink to the compile_commands.json file in
# the source directory, so that it is in a known location and can be picked up
# easily by clangd. When using Visual Studio Code, use the clangd extension,
# version 0.3.2 or higher, so the clangd language server automatically restarts
# on configuration change, which is useful when switching between Debug and
# Release builds (to switch between debugging and testing performance). See
# https://github.com/clangd/vscode-clangd/issues/300. Always build the "ALL"
# target during development, to ensure the compile_commands.json is in sync.
if(CMAKE_EXPORT_COMPILE_COMMANDS)
add_custom_target(
copy_compile_commands ALL
COMMAND ${CMAKE_COMMAND} -E rm -f
"${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json"
COMMAND
${CMAKE_COMMAND} -E create_symlink
"${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json"
"${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json")
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment