VS Code 中 C++ 项目 json 配置说明

以下是关于 Visual Studio Code 中的 tasks.jsonlaunch.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: 任务类型,常用 shellprocess
    • command: 执行的命令,例如 g++
    • args: 命令行参数数组。
      • -g: 生成调试信息。
      • ${workspaceFolder}: 工作区路径变量。
    • group: 任务分组。
      • kind: 任务类型,如 build
      • isDefault: 是否为默认任务。
    • 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: 指定调试器接口模式,gdblldb
    • 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: 配置名称(如 LinuxWin32)。
    • includePath: 头文件搜索路径数组。
    • defines: 预处理器宏定义数组。
    • compilerPath: 编译器路径。
    • cStandard: C 标准版本(如 c11)。
    • cppStandard: C++ 标准版本(如 c++17)。
    • intelliSenseMode: IntelliSense 模式(如 linux-gcc-x64)。

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: 错误波浪线提示 (enableddisabled)。
  • C_Cpp.intelliSenseEngine: IntelliSense 引擎 (DefaultTag Parser)。

通过这些配置文件,你可以更好地控制 VS Code 中 C/C++ 项目的编辑和编译环境。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值