Can the clangd extension work within header files?

93 Views Asked by At

I am using Visual Studio Code with the clangd extension.

clangd successfully uses the compile_commands.json of my build system. However, If I open one of my header files, included headers are not found - since headers are not in the compile_commands.json.

How can I setup clangd for it to find the headers and suggest autocompletions within my headers?

1

There are 1 best solutions below

0
On

If your headers are located in the project directory (the directory that contains the compile_commands.json file), things should generally just work. When opening the header, clangd will find the compile_commands.json file (it looks in ancestor directories of the opened file), and even though it does not contain an entry for the header itself, clangd will pick another entry and use the same flags. (There are edge cases where this heuristic doesn't work, e.g. if different files in the project have different flags, clangd may pick the wrong one.)

If the headers are located outside the project directory, clangd will not find the compile_commands.json for them by default. In this case, a common work around is to instruct clangd to use the compile_commands.json at a specified path for all files opened in the session, as described in this FAQ answer.

If you're not able to get it to work, please feel free to provide more details (e.g. directory structure) and attach clangd logs.