1. 程式人生 > >【VSCode】Windows下VSCode編譯調試c/c++【更新 2018.03.27】

【VSCode】Windows下VSCode編譯調試c/c++【更新 2018.03.27】

記錄 root ret 保存 nload word res base install

————————– 2018.03.27 更新————————-
便攜版已更新,點此獲取便攜版
已知BUG:中文目錄無法正常調試
用於cpptools 0.15.0插件的配置文件更新
新的launch.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file // ${fileBasename}: the current opened file‘s basename // ${fileDirname}: the current opened file‘s dirname // ${fileExtname}: the current opened file‘s extension // ${cwd}: the current working directory of the spawned process { "version": "0.2.0"
, "configurations": [ { "name": "(gdb) Launch", "preLaunchTask": "build", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false
, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:/Program Files (x86)/MinGW/bin/gdb.exe", // GDB的路徑,註意替換成自己的路徑 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }] }

新的tasks.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file‘s basename 
// ${fileDirname}: the current opened file‘s dirname    
// ${fileExtname}: the current opened file‘s extension  
// ${cwd}: the current working directory of the spawned process


{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "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}\\${fileBasenameNoExtension}.exe\""
                ]
            }
        }
    ]
}

懶得自己配置或自己配置出現不明問題的朋友可以點這裏:
【VSCode】Windows下VSCode便攜式c/c++環境
http://blog.csdn.net/c_duoduo/article/details/52083494
下載解壓即可食用。

————————– 2017.06.10 更新 (已過時)————————-

便攜版已更新,點此獲取便攜版
用於cpptools插件的配置文件更新
更新的launch.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file‘s basename 
// ${fileDirname}: the current opened file‘s dirname    
// ${fileExtname}: the current opened file‘s extension  
// ${cwd}: the current working directory of the spawned process
{
    "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": "C:\\MinGW\\bin\\gdb.exe", // miDebugger的路徑,註意這裏要與MinGw的路徑對應

        "args": [], // 程序調試時傳遞給程序的命令行參數,一般設為空即可
        "stopAtEntry": false, // 設為true時程序將暫停在程序入口處,一般設置為false
        "cwd": "${fileDirname}", // 調試程序時的工作目錄,一般為${workspaceRoot}即代碼所在目錄
        "externalConsole": true, // 調試時是否顯示控制臺窗口,一般設置為true顯示控制臺
        "preLaunchTask": "g++"   // 調試會話開始前執行的任務,一般為編譯程序,c++為g++, c為gcc
    }]
}

更新的tasks.json

{
    "version": "0.1.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${file}.exe"],    // 編譯命令參數
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

懶得自己配置或自己配置出現不明問題的朋友可以點這裏:
【VSCode】Windows下VSCode便攜式c/c++環境
http://blog.csdn.net/c_duoduo/article/details/52083494
下載解壓即可食用。

————————– 以下是原文 ————————-

這篇文章為blackkitty記錄在windows下使用vscode編譯調試c/c++的詳細過程

首先看效果
設置斷點,變量監視,調用堆棧的查看
技術分享圖片

條件斷點的使用:
技術分享圖片

下面是配置過程:

總體流程:

  1. 下載安裝vscode
  2. 安裝cpptools插件
  3. 安裝編譯、調試環境
  4. 修改vscode調試配置文件
  5. 完了

下載安裝vscode
https://code.visualstudio.com/Download
技術分享圖片
點擊下載自己喜歡的相應版本,綠色版解壓即可食用

安裝cpptools插件
打開vscode,按ctrl+e打開快速命令框,輸入以下命令後等待

ext install cpptools

vscode在短暫的聯網查找後會列出插件列表,如圖:
技術分享圖片
點擊箭頭所指處的按鈕安裝插件,安裝過程可能會有些慢耐心等待
安裝完成後vscode會提示你重啟vscode,此時重啟即可

安裝編譯、調試環境

目前windows下調試僅支持 Cygwin 和 MinGW。
這裏使用的是MinGW.
下面是MinGW的安裝配置過程:
http://mingw.org/
進入官網點擊右側 Download Installer下載安裝器
打開安裝器點擊install準備安裝:
技術分享圖片

選擇一個安裝目錄,默認為C:\MinGW這裏選擇的是A:\MinGW
點擊Continue開始安裝,安裝過程需聯網,若安裝時提示error則需FQ安裝
安裝過程很快,結束後Continue按鈕恢復為可用狀態,點擊完成安裝。

技術分享圖片
打開MinGW安裝管理器進行進一步配置

技術分享圖片

註意這裏gdb必選,否則無法調試
技術分享圖片
選中幾個需要的項右鍵Make for Installation進行標記,其中gcc和g++為c和c++編譯器
選擇完全部想要安裝的項後點擊左上角Installation菜單下的Apply Changes應用修改,過程需聯網,中間出現error可先繼續,若最後失敗則需FQ更新,建議FQ

然後配置系統環境變量path,這一步為必須
在 我的電腦 上右鍵 屬性:
然後按照下面步驟做即可,註意最後新建的項要與之前MinGW安裝位置相對應
技術分享圖片

修改vscode調試配置文件
再次打開vscode,註意配置系統環境變量path後重啟一下vscode
註意vscode調試需要在打開的文件夾中進行

打開文件夾後,新建test.cpp進行輸入代碼測試:
技術分享圖片

如圖示進入調試界面選擇C++:
技術分享圖片
技術分享圖片

然後會在工作目錄下的生成一個launch.json的啟動配置文件:
技術分享圖片

使用下面代碼替換該文件:

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

註意miDebuggerPath要與MinGw的路徑對應
替換後保存,然後切換至test.cpp,按F5進行調試,此時會彈出一個信息框要求你配置任務運行程序,點擊它~
技術分享圖片
在這裏隨便選一個:

技術分享圖片

然後用下面代碼替換:

{
    "version": "0.1.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${file}.exe"],    // 編譯命令參數
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

保存一下,然後切換至test.cpp,再次按F5啟動調試~
技術分享圖片

完了!

【VSCode】Windows下VSCode編譯調試c/c++【更新 2018.03.27】