以下是关于 Visual Studio Code 中的 tasks.json
和 launch.json
文件针对 C++ 项目的详细说明。
tasks.json
tasks.json
文件用于定义自动化任务,比如编译代码。
基本结构
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${workspaceFolder}/main.cpp",
"-o",
"${workspaceFolder}/main"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
关键字段说明
- version: 配置文件版本。
- tasks: 任务数组。
- label: 任务名称。
- type: 任务类型,常用
shell
或process
。 - command: 执行的命令,例如
g++
。 - args: 命令行参数数组。
-g
: 生成调试信息。${workspaceFolder}
: 工作区路径变量。
- group: 任务分组。
- kind: 任务类型,如
build
。 - isDefault: 是否为默认任务。
- kind: 任务类型,如
- problemMatcher: 用于解析编译错误的模式,
$gcc
针对 GCC 编译器。
launch.json
launch.json
文件用于配置调试器的参数。
基本结构
{
"version": "0.2.0",
"configurations": [
{
"name": "GDB Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": {
"engineLogging": false
}
}
]
}
关键字段说明
- version: 配置文件版本。
- configurations: 配置数组。
- name: 配置名称。
- type: 调试器类型,
cppdbg
用于 C++。 - request: 调试请求类型,
launch
表示启动程序。 - program: 要调试的程序路径。
- args: 传递给程序的命令行参数。
- stopAtEntry: 是否在入口处停止。
- cwd: 工作目录。
- environment: 环境变量数组。
- externalConsole: 是否使用外部控制台。
- MIMode: 指定调试器接口模式,
gdb
或lldb
。 - setupCommands: 调试器启动时执行的命令。
- preLaunchTask: 调试前执行的任务名称。
- miDebuggerPath: 调试器路径。
- logging: 配置日志记录。
c_cpp_properties.json
c_cpp_properties.json
文件用于配置 C/C++ 扩展的 IntelliSense、编译器路径和其他相关设置。
基本结构
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
关键字段说明
- configurations: 配置数组。
- name: 配置名称(如
Linux
、Win32
)。 - includePath: 头文件搜索路径数组。
- defines: 预处理器宏定义数组。
- compilerPath: 编译器路径。
- cStandard: C 标准版本(如
c11
)。 - cppStandard: C++ 标准版本(如
c++17
)。 - intelliSenseMode: IntelliSense 模式(如
linux-gcc-x64
)。
- name: 配置名称(如
settings.json
settings.json
文件用于配置 VS Code 的全局或工作区设置。
示例结构
{
"files.associations": {
"*.cpp": "cpp",
"*.h": "cpp"
},
"editor.formatOnSave": true,
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"C_Cpp.errorSquiggles": "enabled",
"C_Cpp.intelliSenseEngine": "Default"
}
关键字段说明
- files.associations: 文件类型关联。
- editor.formatOnSave: 保存时自动格式化。
- C_Cpp.default.configurationProvider: 配置提供者插件(如
ms-vscode.cmake-tools
)。 - C_Cpp.errorSquiggles: 错误波浪线提示 (
enabled
或disabled
)。 - C_Cpp.intelliSenseEngine: IntelliSense 引擎 (
Default
或Tag Parser
)。
通过这些配置文件,你可以更好地控制 VS Code 中 C/C++ 项目的编辑和编译环境。