英文:
How can I make the VS Code clangd extension aware of the include paths defined in my CMake configuration?
问题
I'm coding C++ on a Raspberry Pi 3B via VSCode's remote ssh. Currently, I just manually add all extra include directories (and other compiler settings) in settings.json
:
{
"clangd.fallbackFlags": [
"-I/usr/include/foo",
"-I/usr/include/bar",
"-I${workspaceFolder}/src",
"-std=c++17"
]
}
And I'm seeking for an automated way to do that.
I'm NOT using the CMake Tools
and IntelliSense
plugin, as it is very CPU and memory consuming, almost exhaust RPi's 1GB memory. In contrast, clangd
is much lighter and capable by a RPi.
英文:
I'm coding C++ on a Raspberry Pi 3B via VSCode's remote ssh. Currently, I just manually add all extra include directories (and other compiler settings) in settings.json
:
{
"clangd.fallbackFlags": [
"-I/usr/include/foo",
"-I/usr/include/bar",
"-I${workspaceFolder}/src",
"-std=c++17"
]
}
And I'm seeking for an automated way to do that.
I'm NOT using the CMake Tools
and IntelliSense
plugin, as it is very CPU and memory consuming, almost exhaust RPi's 1GB memory. In contrast, clangd
is much lighter and capable by a RPi.
答案1
得分: 3
请看 VS Code Clangd 扩展的项目设置文档,其中提到:
> 你必须告诉 clangd 如何构建你的项目(编译标志)。通常可以通过构建系统生成 compile_commands.json
文件(例如,使用 CMake,通过设置 -DCMAKE_EXPORT_COMPILE_COMMANDS=1
)。
> 请查看 clangd 文档中的 项目设置 获取详细信息和备选方法。
在链接的 clangd 文档中,你会看到:
> compile_commands.json
> 此文件为项目中的每个源文件提供编译命令。通常由工具生成。
> clangd 会在你编辑的文件的父目录中查找它,还会在名为 build/
的子目录中查找。例如,如果正在编辑 $SRC/gui/window.cpp
,我们会在 $SRC/gui/
、$SRC/gui/build/
、$SRC/
、$SRC/build/
等位置进行搜索。
由于 CMake 在构建树的根目录生成编译命令数据库,除非你在源码树内构建,否则你可能需要:
- 配置 clangd 以查找构建树(参见 https://clangd.llvm.org/config#compilationdatabase)或
- 手动将文件复制到源码树中。另请参阅 https://stackoverflow.com/q/57464766/11107541(或者可以创建符号链接)。
还请查看 CMAKE_EXPORT_COMPILE_COMMANDS
的文档(请注意,截至撰写此回答时,只有 Ninja 或 Makefiles 生成器支持)。你可能希望将该复制文件的路径添加到你的 .gitignore 中。
对于可能使用 CMake Tools 扩展的其他读者(与提问者不同):文档建议使用 -D...
,如果你通过命令行自己调用配置命令,那么你可以这样做。如果你通过 VS Code CMake Tools 扩展进行配置,你可以使用它贡献的 cmake.configureSettings
设置,或者编写一个 CMake 配置预设 并使用 cacheVariables
属性(因为 CMake Tools 支持 CMake 预设)。
英文:
See the VS Code Clangd extension's Project Setup docs, which state:
> you must tell clangd how your project is built (compile flags). A compile_commands.json file can usually be generated by your build system (e.g. with CMake, by setting -DCMAKE_EXPORT_COMPILE_COMMANDS=1
).
>
> See Project Setup in the clangd documentation for details and alternatives.
In the linked clangd docs, you'll see:
> compile_commands.json
> This file provides compile commands for every source file in a project. It is usually generated by tools.
>
> clangd will look in the parent directories of the files you edit looking for it, and also in subdirectories named build/
. For example, if editing $SRC/gui/window.cpp
, we search in $SRC/gui/
, $SRC/gui/build/
, $SRC/
, $SRC/build/
, …
Since CMake generates the compile commands database in root of the build tree, unless you're doing an in-source build, you'll probably need to either
- configure clangd to look in the build tree (see https://clangd.llvm.org/config#compilationdatabase) or
- copy the file manually to your source tree. See also https://stackoverflow.com/q/57464766/11107541 (or you can create a symlink).
See also the docs for CMAKE_EXPORT_COMPILE_COMMANDS
(note that it's only supported at the time of this writing if you're using a Ninja or Makefiles generator). You'll probably want to add that copy's path to your .gitignore.
Note for other readers who may be using the CMake Tools extension (unlike the asker here): The docs say to use -D...
, which you do if you're calling the configuration command yourself via commandline. If you're doing it through the VS Code CMake Tools extension, you can either use the cmake.configureSettings
setting it contributes, or write a CMake configure preset and use the cacheVariables
property (since CMake Tools supports CMake presets).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论