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 documentation says that the compile_commands.json
is searched next to the file and if not found searched in the parent folders until one is found (thanks to @Friedrich to pointing this to me).
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 file (compile_flags.txt
) 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. (compile_flags.txt
):
-x
c++
-std=c++2b
-D
MYFLAG1
-D
MYFLAG2
-I
include
-I
../project2/include
Remarks:
- with
compile_command.json
the gd
coc command (<Plug>(coc-definition)
) goes to the implementation of the function (.cpp
in general)
- with
compile_flags.txt
the gd
coc command (<Plug>(coc-definition)
) goes to the declaration of the function (.h
in general)
It seems to me that the gi
coc command (<Plug>(coc-definition)
) is not implemented in the coc-clangd
.
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.