问题
在Linux主机或Docker环境下,使用 VS Code 编写 CPP 代码时,变量没有代码补全。
解决方案
首先确保已经安装并启用了 C++
扩展3件套:C/C++、C/C++ Themes、CMake Tools
。
确保设置中的 C_CPP: Intelli Sense Engine
选项为 default
,即开启状态。
在 VS Code 的项目根目录下找到 .vscode
文件夹,若没有可创建此文件夹。
在 .vscode
文件夹中新建文件:c_cpp_properties.json
,编写配置如下:
{
"configurations": [
{
"name": "Linux",
"compilerPath": "/usr/bin/gcc",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
若 .vscode
文件夹中已存在 settings.json
文件,则无需更改;若不存在,可新建此文件,编写配置如下(可自行修改配置):
{
"files.associations": {
"*.inc": "cpp",
"array": "cpp",
"bitset": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"ranges": "cpp",
"span": "cpp",
"regex": "cpp",
"utility": "cpp",
"valarray": "cpp",
"any": "cpp",
"atomic": "cpp",
"barrier": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"coroutine": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"netfwd": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"source_location": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"fstream": "cpp",
"future": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"latch": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"scoped_allocator": "cpp",
"semaphore": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"syncstream": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
},
// "files.autoSave": "afterDelay",
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"editor.tabSize": 2,
"cmake.configureOnOpen": false,
"C_Cpp.formatting": "clangFormat",
"C_Cpp.clang_format_style": "LLVM"
}
保存好配置文件后,重启 VS Code 打开项目,即可修复自动补全功能。