參照這個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
.//下載下傳位址,http://mingw-w64.org/doku.php/download/mingw-builds,建議安裝在c:\Mingw目錄下C:\Mingw-w64
- 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
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/binbin
-
Configure Bash console to use //ctrp+shift+p調出的輸入框裡輸入settings,選擇open settings(json),就會自動寫好一個json檔案,将
{ "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 } ] } ] }"terminal.integrated.shell.windows"這一項改為git的bash.exe的目錄,如下
-
ctrp+shift+p調出的輸入框裡輸入c/,選擇edit configurations,就會自動生成c_cpp_properties.json檔案,之後Find the
setting and paste in the path to thecompilerPath
folder. If you installed Mingw-w64 version 8.1.0 under C:\mingw-w64, the path will look like this:bin
. 以及SetC:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\g++.exe
tointelliSenseMode
. This setting helps the IntelliSense feature provide the correct information for GCC.并删除gcc-x64
setting,如下includePath
{ "configurations": [ { "name": "Win32", "defines": [ "_DEBUG", "UNICODE" ], "compilerPath": "C:/mingw/bin/g++.exe", "intelliSenseMode": "gcc-x64", "browse": { "path": [ "${workspaceFolder}" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" } } ], "version":
-
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; }
轉載于:https://www.cnblogs.com/dogingate/p/11188633.html