使用VScode調試C++

Quick Start

1. 下載插件

在這裏插入圖片描述

2. 寫個HelloWorld的源文件,如main.cpp

#include <iostream>
using namespace std;

int main()
{
    cout << "hello world";
    return 0;
}

3. 準備mingw

CSDN下載: https://download.csdn.net/upload/11867227
有能力翻牆的:https://code.google.com/archive/p/mingw-builds/downloads
如果都覺得麻煩請,留言稍後我下載幾個放到gitee

在這裏插入圖片描述
推薦下載預編譯的mingw,大概這種結構

注意解壓路徑不能有空格,本文解壓在D:\DevProgram\mingw

4. VScode 配置文件

在這裏插入圖片描述

//c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            //修改爲自己的Mingw路徑
            "compilerPath": "D:\\Program Files\\mingw\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}
//launch.json
{
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            //修改爲自己的Mingw路徑
            "miDebuggerPath": "D:\\DevProgram\\mingw\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc.exe build active file"
        }
    ]
}
//tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "gcc.exe build active file",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "${fileBasenameNoExtension}.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

主要修改c_cpp_properties.jsonlaunch.json中Mingw的位置

5. 調試測試

在這裏插入圖片描述

推薦閱讀

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章