1.安装 Clang:
sudo apt update
sudo apt-get install clang
2.安装 “C/C++ Clang Command Adapter” 插件:
在 VS Code 中,打开扩展视图(点击侧边栏的扩展图标或使用快捷键 Ctrl+Shift+X
),搜索并安装官方的 "C/C++ Clang Command Adapter " 扩展。
3.配置 c_cpp_properties.json:
指定 Clang 作为编译器。通常,这个文件位于 .vscode
文件夹中,并且可以通过命令面板(Ctrl+Shift+P
)中的 "C/C++: Edit Configurations (JSON)" 命令来编辑。
{
"configurations": [
{
"name": "Ubuntu",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 1
}
请确保 "compilerPath"
指向正确的 Clang 可执行文件路径。在大多数 Linux 发行版中,Clang 的默认安装路径是 /usr/bin/clang
。
4.配置 tasks.json
:
如果需要自定义构建任务,可以编辑 tasks.json
文件。这个文件也位于 .vscode
文件夹中,可以通过命令面板中的 "Tasks: Configure Task" 命令来创建或编辑。
在 tasks.json
文件中,你可以定义一个任务来使用 Clang 编译你的项目:
{
"version": "1.0.0",
"tasks": [
{
"label": "build clang",
"type": "shell",
"command": "clang",
"args": ["-Wall", "${file}"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
5.配置 launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "Clang Build and Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/your_program",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build clang"
}
]
}
VS Code 的 C/C++ 扩展使用 LLVM Clangd 提供智能感知功能,而 Clang Command Adapter 允许你使用 Clang 作为编译器和调试器。确保你安装了最新版本的 Clang,以获得最佳支持。
Alt+Shift+f
来格式化你的代码,把代码风格统一。