I would have a compile_commands.json
in the root of my project.
I make sure it is the working directory (using vim-rooter may help for that) but I'm not sure it is a requirement.
The compile_commands.json
would contains the instructions to build each of the cpp
files.
For a project like:
._ project1
| |_ main.cpp
|
|_ project2
| |_ foo.cpp
| |
| |_ include
| |_ foo.h
|
|_ compile_commands.json
e.g.(compile_commands.json
):
[
{
"directory": "full/path/to/project1",
"command": "clang++.exe -x c++ main.cpp -std=c++2b -DMYFLAG1 -DMYFLAG2 -Iinclude -I../project2/include",
"file": "main.cpp"
},
{
"directory": "full/path/to/project2",
"command": "clang++.exe -x c++ foo.cpp -std=c++2b -DMYFLAG1 -DMYFLAG2 -Iinclude",
"file": "foo.cpp"
}
]
In order to debug your compile_commands.json
the following command is helpful:
clangd --check=project2/foo.cpp
It allow to to verify that:
- The
compile_commands.json
is correctly identified
- The resulting compilation flag for the selected file (
project2/foo.cpp
in the example)
If that is too much work you could also only have a flag with common compiler flags (it provides already some information about the available includes to clangd). It allow to navigate through the includes and to the declaration but not to the definition.
e.g. (compiler_flags.txt
):
-x
c++
-std=c++2b
-D
MYFLAG1
-D
MYFLAG2
-I
include
-I
../project2/include
compile_commands.json
with e.g.make clean; bear -- make
orcmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1
. See the docs. The autoformatting is a bit of a different question../build/compile_commands.json
file - It still doesn't seem to work. Copying the file to my workspace root directory solved the issue--compile-commands-dir=…
toclangd
as well.