參照這個https://code.visualstudio.com/docs/cpp/config-mingw
中文搜索出來的都語焉不詳,照做一遍都有點問題,按照英文的來一遍ok
-
Install Visual Studio Code.
-
Install the C++ extension for VS Code.//在extensions搜索欄里輸入c/c++,排名最靠前下載量最多的那個插件
-
Install Mingw-w64 to a folder that has no spaces in its path (in other words, NOT the default location of C:/Program Files/). In this tutorial, we assume it is installed under
C:\Mingw-w64
.//下載地址,http://mingw-w64.org/doku.php/download/mingw-builds,建議安裝在c:\Mingw目錄下 -
Install a shell program such as Bash. If you have installed Git for Windows, you already have a Bash shell that the extension can discover and use for its integrated Terminal. If you don't have Git for Windows installed, then you can install bash.exe as part of MSYS2.//建議安裝git
-
In the Windows search box, type "path" and then choose "Edit the system environment variables" from the results list.//就是“搜索程序和文件"這個框里輸入path即可,將系統環境變量path進行編輯
-
Add the paths to your Bash shell and to your mingw-w64
bin
folder to the Windows PATH environment variable. The extension will pass this environment variable to the Bash shell when it opens it.//最后加上c:/Mingw或者c:/Mingw/bin,及c:/git/bin -
Configure Bash console to use //ctrp+shift+p調出的輸入框里輸入settings,選擇open settings(json),就會自動寫好一個json文件,將
"terminal.integrated.shell.windows"這一項改為git的bash.exe的目錄,如下
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",全文如下
{"version": "0.2.0","configurations": [{"name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "${workspaceFolder}/helloworld.exe","args": [],"stopAtEntry": true,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "C:/MinGW/bin/gdb.exe","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]} -
ctrp+shift+p調出的輸入框里輸入c/,選擇edit configurations,就會自動生成c_cpp_properties.json文件,之后Find the
compilerPath
setting and paste in the path to thebin
folder. If you installed Mingw-w64 version 8.1.0 under C:\mingw-w64, the path will look like this:C:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\g++.exe
. 以及SetintelliSenseMode
togcc-x64
. This setting helps the IntelliSense feature provide the correct information for GCC.並刪除includePath
setting,如下{ "configurations": [ { "name": "Win32", "defines": [ "_DEBUG", "UNICODE" ], "compilerPath": "C:/mingw/bin/g++.exe", "intelliSenseMode": "gcc-x64", "browse": { "path": [ "${workspaceFolder}" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" } } ], "version": 4 }
-
ctrp+shift+p調出的輸入框里輸入task,並選擇add a default build task,自動創建tasks.json文件,如下
{ "version": "2.0.0", "tasks": [ { "label": "build hello world", "type": "shell", "command": "g++", "args": [ "-g", "-o", "helloworld", "helloworld.cpp" ], "group": { "kind": "build", "isDefault": true } } ] }
- 創建一個helloworld.cpp文件,並用ctrl+shitf+b進行編譯后,可設置斷點進行調試,代碼如下
#include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<string> msg {"Hello", "C++", "World", "from", "VS Code!"}; for (const string& word : msg) { cout << word << " "; } cout << endl; }