VSCode進行c++代碼調試


步驟

  1. 新建一個文件
  2. 添加c_cpp_properties.json、launch.json、tasks.json文件
  3. 寫代碼開始調試

json腳本范例

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                //TODO: 根據情況修改
                "E:/MinGW/lib/gcc/mingw32/6.3.0/include/c++",
                "E:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/mingw32",
                "E:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/backward",
                "E:/MinGW/lib/gcc/mingw32/6.3.0/include",
                "E:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/tr1",
                "E:/MinGW/mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    //TODO: 根據情況修改
                    "E:/MinGW/lib/gcc/mingw32/6.3.0/include/c++",
                    "E:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/mingw32",
                    "E:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/backward",
                    "E:/MinGW/lib/gcc/mingw32/6.3.0/include",
                    "E:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/tr1",
                    "E:/MinGW/mingw32/include"
                ]
            },
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": "",
            "compilerPath": "E:/VS2015/VC/bin/cl.exe",   //TODO: 根據情況修改
            "cStandard": "c11", //TODO: 根據情況修改
            "cppStandard": "c++11"      //TODO: 根據情況修改
        }
    ],
    "version": 4
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "C++ Launch (GDB)", // 配置名稱,將會在啟動配置的下拉菜單中顯示
            "type": "cppdbg", // 配置類型,這里只能為cppdbg
            "request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加)
            "targetArchitecture": "x86", // 生成目標架構,一般為x86或x64,可以為x86, arm, arm64, mips, x64, amd64, x86_64
            "program": "${file}.exe", // 將要進行調試的程序的路徑
            "miDebuggerPath": "E:\\MinGW\\bin\\gdb.exe", // miDebugger的路徑,注意這里要與MinGw的路徑對應, //TODO: 根據情況修改
            "args": [
                "blackkitty",
                "1221",
                "# #"
            ], // 程序調試時傳遞給程序的命令行參數,一般設為空即可
            "stopAtEntry": false, // 設為true時程序將暫停在程序入口處,一般設置為false
            "cwd": "${workspaceRoot}", // 調試程序時的工作目錄,一般為${workspaceRoot}即代碼所在目錄
            "externalConsole": true, // 調試時是否顯示控制台窗口,一般設置為true顯示控制台
            "preLaunchTask": "g++" // 調試會話開始前執行的任務,對應tasks.json中的label名
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++", //這個與lauch.json中的preLaunchTask要保持一致
            //"type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${file}.exe"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },

            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM