一、環境
1、mingw64
由於在線安裝版速度很慢,此處選擇離線版安裝
下載地址:https://sourceforge.net/projects/mingw-w64/files/

具體版本的選擇參考下圖(引用自https://blog.csdn.net/jiqiren_dasheng/article/details/103775488)

選擇下載

這個版本,如果下載慢,可以在網頁上選擇不同的下載鏡像
下載后解壓到某個目錄。然后在環境變量中的PATH中增加解壓后的bin目錄即可
2、vscode
下載vscode,解壓運行后,在應用市場中安裝如下插件:




插件的使用參看:https://zhuanlan.zhihu.com/p/92175757
重啟后生效
二、配置
首先在vscode中打開一個文件夾作為工作目錄。
在工作目錄下先建一個測試文件:
1 #include <stdio.h>
2 int main()
3 { 4 printf("你好,世界\n"); 5 return 0; 6 }
然后快捷鍵執行ctrl+shift+p,輸入“c/c++”打開環境變量配置窗口:

默認打開的內容如下:
1 { 2 "configurations": [ 3 { 4 "name": "Win32", 5 "includePath": [ 6 "${workspaceFolder}/**" 7 ], 8 "defines": [ 9 "_DEBUG", 10 "UNICODE", 11 "_UNICODE" 12 ], 13 "compilerPath": "D:\\green\\mingw64\\bin\\gcc.exe", 14 "cStandard": "gnu17", 15 "cppStandard": "gnu++14", 16 "intelliSenseMode": "windows-gcc-x64" 17 } 18 ], 19 "version": 4 20 }
13行是gcc的安裝目錄。保存后會在當前文件夾下生成一個目錄,名稱為.vscode,里面保存當前工作空間的配置信息。
下面生成launch.json:

繼續選擇:


在彈出框中選擇:

會自動添加如下內容:
1 { 2 // 使用 IntelliSense 了解相關屬性。 3 // 懸停以查看現有屬性的描述。 4 // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387 5 "version": "0.2.0", 6 "configurations": [ 7 { 8 "name": "(gdb) 啟動", 9 "type": "cppdbg", 10 "request": "launch", 11 "program": "輸入程序名稱,例如 ${workspaceFolder}/a.exe", 12 "args": [], 13 "stopAtEntry": false, 14 "cwd": "${fileDirname}", 15 "environment": [], 16 "externalConsole": false, 17 "MIMode": "gdb", 18 "miDebuggerPath": "/path/to/gdb", 19 "setupCommands": [ 20 { 21 "description": "為 gdb 啟用整齊打印", 22 "text": "-enable-pretty-printing", 23 "ignoreFailures": true 24 } 25 ] 26 } 27 28 29 ] 30 }
將以上內容修改如下:
1 { 2 "version": "0.2.0", 3 "configurations": [ 4 5 { 6 "name": "(gdb) Launch", // 配置名稱,將會在啟動配置的下拉菜單中顯示 7 "type": "cppdbg", // 配置類型,這里只能為cppdbg 8 "request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加) 9 "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 將要進行調試的程序的路徑 10 "args": [], // 程序調試時傳遞給程序的命令行參數,一般設為空即可 11 "stopAtEntry": false, // 設為true時程序將暫停在程序入口處,一般設置為false 12 "cwd": "${workspaceRoot}",// 調試程序時的工作目錄,一般為${workspaceRoot}即代碼所在目錄 13 "environment": [], 14 "externalConsole": true,// 調試時是否顯示控制台窗口,一般設置為true顯示控制台 15 "MIMode": "gdb", 16 "miDebuggerPath": "D:\\green\\mingw64\\bin\\gdb64.exe",// miDebugger的路徑,注意這里要與MinGw的路徑對應 17 "preLaunchTask": "gcc", // 調試會話開始前執行的任務,一般為編譯程序,c++為g++, c為gcc 18 "setupCommands": [ 19 { 20 "description": "Enable pretty-printing for gdb", 21 "text": "-enable-pretty-printing", 22 "ignoreFailures": true 23 } 24 ] 25 } 26 ] 27 }
miDebuggerPath 這一條,要與你mingw64安裝路徑一致,注意在路徑中 '\'要替換為'\\'
保存launch.json后,嘗試執行程序:

會出現如下錯誤:

說明需要建立task.json文件


生成的內容如下:
1 { 2 // See https://go.microsoft.com/fwlink/?LinkId=733558 3 // for the documentation about the tasks.json format 4 "version": "2.0.0", 5 "tasks": [ 6 { 7 "label": "build", 8 "type": "shell", 9 "command": "msbuild", 10 "args": [ 11 // Ask msbuild to generate full paths for file names. 12 "/property:GenerateFullPaths=true", 13 "/t:build", 14 // Do not generate summary otherwise it leads to duplicate errors in Problems panel 15 "/consoleloggerparameters:NoSummary" 16 ], 17 "group": "build", 18 "presentation": { 19 // Reveal the output only if unrecognized errors occur. 20 "reveal": "silent" 21 }, 22 // Use the standard MS compiler pattern to detect errors, warnings and infos 23 "problemMatcher": "$msCompile" 24 } 25 ] 26 }
