0x00 前言

现在的新版本VSCode在启动C/C++调试的时候不会自动创建tasks.json,会出现提示找不到xxx.exe或者xxx.out的问题。本文从0开始配置vscode的C/C++调试配置。linux与windows都有相应教程。

0x01 Windows

  1. 下载mingw并且安装gcc g++ gdb
  2. 在vscode打开的文件夹下新建.vscode文件夹
  3. 创建tasks.json 并且把以下内容复制到tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "C/C++: g++.exe build active file",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "windows": {
                "command": "g++",
                "args": [
                    "-ggdb",
                    "\"${file}\"",
                    "--std=c++11",
                    "-o",
                    "\"${fileDirname}\\out\\${fileBasenameNoExtension}.exe\""
                ]
            }
        }
    ]
}
  1. 创建launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\out\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}
  1. 然后就可以按f5进行调试了。

0x02 Linux

  1. 首先安装编译器我的ubuntu编译环境

  2. 同windows,创建.vscode文件夹

  3. 创建 c_cpp_properties.json

    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "${workspaceFolder}"
                ],
                "defines": [],
                "compilerPath": "/usr/bin/gcc",
                "cStandard": "c11",
                "cppStandard": "c++17",
                "intelliSenseMode": "gcc-x64"
            }
        ],
        "version": 4
    }
  4. 创建launch.json

    {
        // 使用 IntelliSense 了解相关属性。 
        // 悬停以查看现有属性的描述。
        // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [   
            {
                "name": "(gdb) 启动",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/out/${fileBasenameNoExtension}.out",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "为 gdb 启用整齐打印",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "build"
            }
        ]
    }
  5. 创建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", 
                "command": "g++", 
                "args": [
                    "${file}",
                    "-o", 
                    "${fileDirname}/out/${fileBasenameNoExtension}.out",
                    "-g", 
                    "-static-libgcc", 
                    "-std=c++17" 
                ], 
                "type": "shell", 
                "group": {
                    "kind": "build",
                    "isDefault": true 
                },
                "presentation": {
                    "echo": true,
                    "reveal": "always", 
                    "focus": false, 
                    "panel": "shared" 
                },
                "problemMatcher": "$gcc" 
            }
        ]
    }
  6. 完成