VSCode的C++环境配置,多cpp在同一文件夹(json方式)

插件安装、json编写参考此博客。

略有更改,以支持多个cpp在同一个文件夹中的情况。

主要是tasks.json的args设置,launch.json的program路径设置。

launch.json内容,执行程序用

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",            
            "program": "${fileDirname}/${fileBasenameNoExtension}.out", //要生成的可执行程序
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],            
            "externalConsole": true, //允许打开终端
            "MIMode": "gdb",
            "preLaunchTask": "build", //在启动之前,先执行tasks.json(编译、链接)
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json内容,编译、链接用

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build", //与launch.json中"preLaunchTask": "build",一致
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${fileDirname}/**cpp", //单个、多个cpp文件都可以
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out", //与launch.json的"program"对应
                "-std=c++17" //c++版本
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        //"panel": "shared",
        //"showReuseMessage": true,
        //"clear": false
    }
}
原文地址:https://www.cnblogs.com/xixixing/p/13228950.html