CPP
概述
- 在
VS Code里用C++,主要涉及三个配置文件,都放在项目根目录的.vscode/文件夹下
tasks.json
- 定义"任务"(怎么编译)
- 作用是告诉
VS Code如何执行外部命令,最常见的用途是编译label:任务名字,供其他地方引用(比如launch.json的preLaunchTask)command+args:实际执行的编译命令problemMatcher:把编译器报错解析成VS Code的"问题"面板,可以点击跳转
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "g++", "args": [ "-g", // 生成调试信息 "-std=c++17", "${file}", // 当前打开的文件 "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] } |
- 触发方式:
Ctrl+Shift+B(运行默认build任务)- 或命令面板里的
Tasks: Run Task
launch.json
- 定义"调试会话"(怎么运行/调试)
- 作用是告诉调试器(
gdb/lldb)启动哪个可执行文件、怎么启动program:要调试的可执行文件路径,必须和tasks.json里编译出的产物路径一致preLaunchTask:自动关联tasks.json,按F5时先编译再调试MIMode:调试器类型(gdb/lldb)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "version": "0.2.0", "configurations": [ { "name": "debug", "type": "cppdbg", // 或 lldb / cppvsdbg(Windows) "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "MIMode": "gdb", "preLaunchTask": "build" // 关键:调试前先跑这个 tasks.json 里的任务 } ] } |
两者的分工关系
tasks.json负责"编译出可执行文件"launch.json负责"运行/调试这个可执行文件",通过preLaunchTask串起来- 按
F5= 先跑build task,再启动调试器
- 按
c_cpp_properties.json
- 定义"智能感知"(不涉及编译/运行)
- 这个常被忽略,但很重要,作用是让
C/C++插件的IntelliSense(自动补全、跳转定义、报错提示)能找到头文件、识别语言标准
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/include" ], "defines": [], "compilerPath": "/usr/bin/g++", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "linux-gcc-x64" } ] } |
- 它不影响实际编译,只影响编辑器里的提示是否准确
- 比如你
#include <vector>能不能跳转过去,红波浪线报不报错 - 可以用
Ctrl+Shift+P→C/C++: Edit Configurations (UI)生成
- 比如你
WSL2+Debian
vscode编码规范
debian安装clang-format
|
1 |
sudo apt install clang-format |
- 在项目根目录创建
.clang-format
|
1 |
BasedOnStyle: Chromium |
wsl:debian安装C/C++扩展- 配置
VS Code的settings.json
|
1 2 3 4 5 6 7 8 9 10 11 12 |
{ "C_Cpp.clang_format_style": "file", "C_Cpp.formatting": "clangFormat", "editor.formatOnSave": true, "editor.defaultFormatter": "ms-vscode.cpptools", "[cpp]": { "editor.defaultFormatter": "ms-vscode.cpptools" }, "[c]": { "editor.defaultFormatter": "ms-vscode.cpptools" } } |
wsl:debian插件
C/C++clangdCodeLLDB
声明:本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!