vscode配置clangd
1 在VScode中安装代码自动补全功能
分两个步骤,首先在VSCode中安装clangd,其次通过shell安装clangd,之后在VSCode的.vscdoe/settiings.json中设置clangd的信息,具体步骤如下所示
安装VSCode插件:
Ctrl+Shift+X,查找clangd进行安装
这里的xenial为Ubuntu16,对应来llvm为11
sudo add-apt-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial main"
sudo add-apt-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-11 main"
sudo apt-get update
sudo apt-get install clangd-11
在开发Vscode的设置:.vscode/settings.json,添加如下内容:
"clangd.path": "/usr/bin/clangd-11",
"C_Cpp.intelliSenseEngine": "Disabled",
"C_Cpp.autocomplete": "Disabled",
重启VSCode,就可以实现代码补全功能
以上方法适用于,无法使用如下的情况
sudo apt-get install clangd
或者
sudo apt-get install clang-tools # clang-tools中包含clangd
2 在settings.json中配置如下:
-
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
: 生成compile_commands.json
-
settings.json
中配置如下信息{ "clangd.arguments": [ "--background-index=false", // 改为false,明显加快outline的加载 "--pch-storage=memory", // 添加该选项,明显加快outline的加载 "--log=error", "--header-insertion=never", "--clang-tidy", "--cross-file-rename=true", // -DCMAKE_EXPORT_COMPILE_COMMANDS=ON "--compile-commands-dir=${workspaceFolder}/build" ], "clangd.path": "/usr/bin/clangd-11", "clangd.syncFileEvents": true, "C_Cpp.intelliSenseEngine": "Disabled", "C_Cpp.autocomplete": "Disabled", "files.maxMemoryForLargeFilesMB": 4096, // 增加内存对打文件的支持 }
3 demo
{
"files.exclude": { // 排除文件和文件夹,使它们在资源管理器中不可见
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/.metadata": true,
// "**/.cache": true,
"**/build": true,
"**/Install": true,
"**/build_test": true,
"**/build_debug": true,
"**/build_release": true,
"**/build_release_g": true,
"**/.cache": true,
"**/.clang-format": true,
"**/archiver/script/deepintemp/**": true,
"**/build_debug_socket": true
},
"search.exclude": { // 排除文件和文件夹,使它们不在搜索结果中不可见
// "**/node_modules": true,
// "**/bower_components": true,
"**/.git": true,
"**/build": true,
"**/build_arm": true,
"**/build_cross": true,
"**/build_debug": true,
"**/build_debug_socket": true
},
"files.watcherExclude": { // 排除文件和文件夹,使它们不被文件监视器监控,从而减少性能开销,赞
// "**/.git/objects/**": true,
// "**/.git/subtree-cache/**": true,
// "**/node_modules/**": true,
"**/build/**": true,
"**/build_arm/**": true,
"**/build_cross/**": true,
"**/build_debug/**": true,
"**/build_debug_socket/**": true,
"**/.metadata/**": true,
},
// "clangd.arguments": [
// "--background-index",
// "--log=error",
// "--header-insertion=never",
// "--clang-tidy",
// "--cross-file-rename=true"
// ],
// "clangd.path": "/usr/bin/clangd-11",
// "clangd.syncFileEvents": true,
// "C_Cpp.intelliSenseEngine": "Disabled",
// "C_Cpp.autocomplete": "Disabled",
"files.associations": {
"iostream": "cpp",
},
"files.autoSaveDelay": 1000,
"files.autoSave": "afterDelay",
"editor.tabSize": 2,
"editor.insertSpaces": true,
"markdown.preview.fontSize": 18
}