目錄
一、下載mingw並配置gcc
下載地址:https://sourceforge.net/projects/mingw-w64/files/
下載的文件:進入網站后不要點擊 "Download Lasted Version",往下滑,找到最新版的 "x86_64-posix-seh"。
下載完成之后解壓。
將bin目錄添加到環境變量path
。


打開cmd運行 g++
出現:
g++: fatal error: no input files
則安裝成功。
否則安裝失敗。
二、vscode的配置
1. 安裝vscode c/c++
插件

你可以閱讀Get Start with C++ Mingw-w64。
這是關於這個插件如何配置的英文文檔。
或者你可以繼續看接下來的內容,為簡略中文版。
2. 創建一個文件夾project
作為你的c++工作區域
創建一個文件夾project
作為你的c++工作區域。
在vscode中打開這個文件夾,並新建一個test.cpp
文件。
在test.cpp
中任意輸入一段代碼。
#include <iostream>
using namespace std;
int main()
{
cout << "hello" << endl;
system("pause");
return 0;
}
3.修改launch.json
文件和task.json
文件
點擊頂部菜單欄中 終端
- 配置默認生成任務
選擇 g++.ext buid and debug active file
,如圖:
此時會彈出task.json
,不必修改,回到test.cpp
頁面。
點擊F5
,此時會再讓你選擇一次,請同樣選擇 g++.ext buid and debug active file
。
此時會彈出 launch.json
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和調試活動文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true, //修改此項,讓其彈出終端
"MIMode": "gdb",
"miDebuggerPath": "D:\\Mingw\\bin\\gdb.exe",
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
修改 externalConsole
參數為 true
。含義為讓其彈出終端。
注:launch.json
中的 preLaunchTask
參數必須與 task.json
中的label
參數相同!
如果不知道其含義,保持默認值不修改即可!
此時再次回到代碼頁面,摁下F5后成功運行!