Skip to content

Instantly share code, notes, and snippets.

@danngreen
Created November 24, 2020 20:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danngreen/9699fbc4471c547dc64104ea5f70f8b6 to your computer and use it in GitHub Desktop.
Save danngreen/9699fbc4471c547dc64104ea5f70f8b6 to your computer and use it in GitHub Desktop.
Makefile command to generate compile_commands.json with entries for c++ headers
# Addresses an issue where language servers won't know what to do with header files
# since compile_commands.json typically only includes .cc/.cpp/.c files.
#
# Include this file from your main Makefile, or just paste the snippet in.
# To use:
# make compile_commands
# Requires bear and compdb
# bear [osx]: brew install bear
# compdb [see https://github.com/Sarcasm/compdb]: pip install compdb
# Assumes build files are in build/ and that `make all` builds the entire project
compile_commands:
rm -rf build
bear make -j16 all
compdb -p ./ list > compile_commands_with_headers.json 2>/dev/null
rm compile_commands.json
mv compile_commands_with_headers.json compile_commands.json
@danngreen
Copy link
Author

Better than bear is compiledb ( https://github.com/nickdiego/compiledb )

compile_commands:
	compiledb make
	compdb -p ./ list > compile_commands_with_headers.json 2>/dev/null
	rm compile_commands.json 
	mv compile_commands_with_headers.json compile_commands.json

@thu0x31
Copy link

thu0x31 commented Feb 26, 2021

It worked well
thnaks!

@danngreen
Copy link
Author

Update: Recent versions of clangd (12 and later) seem to work well without compdb. So, this is all that's needed:

compile_commands:
	compiledb make

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment