AMIR REZA SADEQI
AMIR REZA SADEQI

Reputation: 601

Is there "includePath" option in clangd?

I used to work with VSCode C/C++ extension. there was a feature in this extension(in a json file), called "includePath", which I could set the paths for my headers, so without execution of CMake or make, I would have the suggestion of my headers and code completion from those.

now I have switched to neovim and clangd as the language server for code completion. I searched a lot to find the corresponding feature in clangd options but I could not find anything more than this link.

since the clangd is a powerful language server, I am in wonder if there is not such a feature in it. so I want to know is there actually such a feature in clangd? and if YES how can I use that?

Note: I use a language client, called "coc-clangd". I don't know if it matters or not.

Upvotes: 34

Views: 70651

Answers (6)

Sylvain Chiron
Sylvain Chiron

Reputation: 290

Look at the bottom of the Getting Started page of clangd’s website. The easy solution is to have a compile_flags.txt file and put your -I flags in it.

This is what I’m using for the GenGraph project. We don’t use CMake, we only have a Makefile. The project is divided in four subprojects like this:

src/
└── clih/
    └── *.c, *.h
    gengraph/
    └── *.c, *.h
    gengraph-cli/
    └── *.c, *.h
    gengraph-formats/
    └── *.c, *.h
    compile_flags.txt

And src is the include path, e.g. in files of GenGraph CLI you have:

#include <gengraph/algos/algos.h>

Thus I created a compile_flags.txt in the src/ directory containing the line:

-I.

Upvotes: 0

Aincvy
Aincvy

Reputation: 549

Maybe this is useful: https://clangd.llvm.org/config

Create a file called '.clangd' in the top-level of the source directory. Add those content.

CompileFlags: # Tweak the parse settings, example directory given to show format
  Add:
    # - "-I=[folder]"         # old answer
    - "-I[folder]"            # e.g `-I/usr/include/SDL2`  suggested from @Palmkeep

But I think this is not recommend, all include directories should be add in CMakeLists.txt file.

Upvotes: 27

Maksym
Maksym

Reputation: 51

You can use CPATH environment variable. Syntax is the same as PATH variable.

export CPATH="your/include:more/include:/usr/include"

Upvotes: 4

linwownil
linwownil

Reputation: 109

To use code completion provided by Clangd, let Clangd retrieve include paths from compiler_commands.json with compiler calls used by CMake. Set the CMAKE_EXPORT_COMPILE_COMMANDS option in CMakeLists.txt, it will output compiler_commands.json to the build directory when CMake is run:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Copy the generated compiler_commands.json to the project source directory. Clangd will now source this file.

Upvotes: 8

Allen Shaw
Allen Shaw

Reputation: 1492

You can add includePath to clangd.fallbackFlags into vscode's settings.json like this:

"clangd.fallbackFlags": [
    "-I${workspaceFolder}/include",
    "-I/my/include"
]

Upvotes: 21

lufterd
lufterd

Reputation: 464

Clangd uses compile_commands.json database file which contains flags (such as include directories) for each file in project. But this file is auto-generated, so all modifications to it will be overwritten eventually. You can ask CMake to add any custom compile flags with -DCMAKE_CXX_FLAGS command line argument.

Example for system headers (#include <file.h>):

cmake -DCMAKE_CXX_FLAGS="-isystem /path/to/includes" /path/to/source

For project headers (#include "file.h"):

cmake -DCMAKE_CXX_FLAGS=-Ipath/to/includes /path/to/source

Additionally, you can set CXXFLAGS environment variable:

export CXXFLAGS="-isystem /path/to/includes"
cmake path/to/sources

After that new flags should appear in your compile_commands.json file.

Upvotes: 16

Related Questions