安裝路徑非常建議用預設路徑就好(預設為C槽)(這樣下面的步驟才更不容易出錯!!)
透過MSYS2我們可以獲取最新版本的 Mingw-w64 ,它提供了 GCC、Mingw-w64 和其他有用的 C++ 工具和庫的最新本地構建。您可以從 MSYS2 頁面下載最新的安裝程序。
輸入指令 : pacman -S --needed base-devel mingw-w64-x86_64-toolchain
(這串指令是安裝Mingw-w64的工具鏈)
(下圖為輸入指令後並安裝完Mingw-w64的樣子)
打開你的命令提示字元並輸入 :
gcc --version
g++ --version
gdb --version
如果上面三個指令系統都有成功回應則代表成功安裝,但若某一項指令(或以上)Error則代表你可能要重回上面的步驟看哪一部分漏掉了沒安裝好
(進去後應該會像下圖一樣)
(如下圖)
如果前面你的MSYS2路徑並未手動改變(而是預設路徑),那麼新增的路徑為 : C:\msys64\mingw64\bin
記得在選擇前要先創立好想要做為工作區的資料夾
以我自己來舉例,我將C這個資料夾選擇為我的工作區(如下圖)
先暫時忽略.vscode下面的檔案
c_cpp_properties.json
, launch.json
, tasks.json
, settings.json
共4個.json檔案新增完後會像下圖一樣
前提為上面的步驟都和我做得一樣,特別是安裝路徑
c_cpp_properties.json(編譯器路徑和 IntelliSense 設置)
{
"configurations": [
{
"name": "GCC",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/msys64/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
launch.json(Debugger設置)
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json(構建說明)
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/msys64/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
settings.json(可寫可不寫)
這串程式碼的功能是系統自動幫忙刪除.exe檔,不然每次執行完一次程式都要手動刪除exe檔才可以執行下一次程式
{
"files.exclude": {
"**/*.exe":true,
},
}
這裡要記得加後綴.c(如果是C++則是.cpp)
並且盡量別建立在.vscode裡面,而是建立在工作區最外層(在這裡是C資料夾中)
#include <stdio.h>
int main()
{
printf("Hello World !");
}
點擊Debug C/C++ File(中斷點設在line 5)
假如你的程式執行和下圖一樣,則代表你成功了!
點擊 Run C/C++ File
因為上面我環境建置的原因,所以執行程式後會直接在VS code的終端機(TERMINAL)執行(如下圖)
若是C++的工作區也是同理,可以直接把.vscode資料夾照搬過去
原本的程式碼儲存都需要使用CTRL+S,但你也可以點選File >> Auto Save,系統就會自動幫你儲存程式碼
將launch.json
中的"externalConsole"
改為true
但這樣改完後會產生一個問題,就是小黑窗會一閃而逝
原因為小黑窗輸出資料後就會直接關閉
這裡一樣用Hello World程式碼來舉例
#include <stdio.h>
#include <windows.h>
int main()
{
printf("Hello World !");
system("pause");
}
新增庫函數<windows.h>以及system("pause");
強制小黑窗輸出後暫停,而不結束
直接在程式碼最後加上system("pause");即可
HackMD教學筆記