clangd 中有“includePath”选项吗?

AMI*_*EQI 22 c c++ neovim clangd

我曾经使用 VSCode C/C++扩展。这个扩展中有一个功能(在 json 文件中),称为“includePath”,我可以设置标头的路径,因此无需执行 CMake 或 make,我就会从这些中获得标头和代码完成的建议。

现在我已经切换到 neovim 和 clangd 作为代码完成的语言服务器。我进行了大量搜索以在 clangd options 中找到相应的功能,但除了此链接之外我找不到任何其他内容。

既然clangd是一个强大的语言服务器,我想知道它是否没有这样的功能。所以我想知道clangd中是否真的有这样的功能?如果是的话我该如何使用它?

注意:我使用一个名为“coc-clangd”的语言客户端。我不知道这是否重要。

luf*_*erd 15

Clangd 使用compile_commands.json数据库文件,其中包含项目中每个文件的标志(例如包含目录)。但该文件是自动生成的,因此对其进行的所有修改最终都会被覆盖。-DCMAKE_CXX_FLAGS您可以要求 CMake 使用命令行参数添加任何自定义编译标志。

系统标头 ( ) 的示例#include <file.h>

cmake -DCMAKE_CXX_FLAGS="-isystem /path/to/includes" /path/to/source
Run Code Online (Sandbox Code Playgroud)

对于项目标题 ( #include "file.h"):

cmake -DCMAKE_CXX_FLAGS=-Ipath/to/includes /path/to/source
Run Code Online (Sandbox Code Playgroud)

此外,您可以设置CXXFLAGS环境变量:

export CXXFLAGS="-isystem /path/to/includes"
cmake path/to/sources
Run Code Online (Sandbox Code Playgroud)

之后,新的标志应该出现在您的compile_commands.json文件中。

  • 仅供参考,如果您需要生成“compile_commands.json”,您可以使用“bear -- make”来生成它。 (3认同)

Ain*_*cvy 14

也许这很有用: https: //clangd.llvm.org/config

在源目录的顶层创建一个名为“.clangd”的文件。添加这些内容。

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
Run Code Online (Sandbox Code Playgroud)

但我认为这不推荐,所有包含目录都应该添加到CMakeLists.txt文件中。

  • 注意:如果您在 Visual Studio Code 中使用 clangd,则必须在 VS Code 设置中将 `--enable-config` 添加到 `Clangd: Arguments` 才能使用 .clangd 文件 [来源](https://blogs. igalia.com/aboya/2021/10/02/setting-up-visualstudio-code-to-work-with-webkitgtk-using-clangd/)。我还发现 `"-I=path/to/dir"` 不起作用,但 `"--include-directory=path/to/dir"` 起作用。 (7认同)

All*_*haw 11

你可以像这样添加includePathclangd.fallbackFlagsvscode 中settings.json

"clangd.fallbackFlags": [
    "-I${workspaceFolder}/include",
    "-I/my/include"
]
Run Code Online (Sandbox Code Playgroud)


小智 7

compiler_commands.json要使用 Clangd 提供的代码完成功能,请让 Clangd 从CMake 使用的编译器调用中检索包含路径。在 中设置CMAKE_EXPORT_COMPILE_COMMANDSCMakeLists.txt选项,它会compiler_commands.json在 CMake 运行时输出到构建目录:

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Run Code Online (Sandbox Code Playgroud)

将生成的文件复制compiler_commands.json到项目源码目录下。Clangd 现在将获取该文件。