轉載來自:https://my.oschina.net/u/3669041/blog/1838710/print
最近迷上了vscode這款編譯器,小巧美觀,用起來也很順手,最主要的是全平台,正好最近要上手做Linux C用戶端,以前沒接觸過linux,先拿它先在WIN上練練手。
這幾天在網上找了非常多的教程,win總是配不好環境。(linux和win一起開始配的,感覺linux環境比win好配多了,下一篇博文再把linux端的配置過程分享出來,這裡先寫win的)。
環境:
WIN10 64 專業版
vscode版本:1.24.1
launch.json版本:0.2.0
tasks.json版本:2.0.0
mingw-w64版本:8.1.0
過程:
一、 安裝vscode
vscode官網下載下傳安裝包直接安裝即可

二、 vscode内安裝C/C++ 插件
vscode内按快捷組合鍵Ctrl+Shift+X(或如圖第①步點選[拓展]按鈕)打開拓展分頁,在搜尋欄輸入" C ",查找到如圖的第一個插件,安裝并重新加載之。
三、 安裝mingw-w64(具體安裝與環境變量配置可以檢視這裡)
在mingw-w64官網下載下傳64位的mingw-w64線上安裝包(以線上包為例)或離線包(離線包直接解壓出來就能用)
線上包:根據系統選擇合适的安裝包進行下載下傳,選擇線上安裝器
下載下傳完成後出現如下安裝包
安裝該包,在Setting 界面将Architecture選項改為x86_64,其他不變,選擇合适的安裝路徑(預設或重新指定都可以,路徑中不要有中文)
配置計算機環境變量如圖(我的安裝路徑是D:\mingw64,是以環境變量這麼加)
安裝完成後打開控制台,分别輸入 g++ --version 和 gcc --version 檢視環境是否安裝成功(是否有目前版本号)
四、重新開機電腦(這裡看了其他很多部落客的沒有提到,我沒有重新開機,後來vscode代碼寫出來跑了很多次提示沒有找到g++指令,最後重新開機解決)
五、運作C++代碼
打開vscode,選擇或建立一個空檔案夾目錄打開作為項目目錄,建立一個test.cpp檔案,鍵入如下helloworld代碼
#include <stdio.h>
int main(int argc, char *args[])
{
int i, j;
printf("hello world!\n");
printf("argc:%d\nargv:\n", argc);
for (i = 0; i < argc; i++)
{
printf("%d:%s\n", i, args[i]);
}
getchar();
return 0;
}
按下F5,頂部或出現如下菜單,選擇C++(GDB/LLDB)
系統自動在目前目錄下建立.vscode檔案夾,并在其中建立一個launch.json的模版檔案如下:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以檢視現有屬性的描述。
// 欲了解更多資訊,請通路: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
将該模版修改為如下(可以直接複制,并修改有注釋的一段)
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "build",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe", // 這裡修改GDB路徑為安裝的mingw64的bin下的gdb.exe路徑
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}]
}
vscode中按下組合鍵Shift+Ctrl+P,在喚出的工作列中鍵入>task,下拉找到并點選 Tasks:Configure Task(任務:配置任務)項,并在接下來的傳回項中選擇使用模版建立tasks.json檔案
系統會自動在.vscode檔案夾下建立一個tasks.json檔案,自動生成的代碼如下
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
同理,将之修改為如下代碼(可直接覆寫)
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
"\"${file}\"",
"--std=c++11",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
]
}
}
]
}
至此,環境配置完成,轉到C++代碼頁,按下F5,根目錄下出現.cpp檔案同名的.exe檔案,代碼自動執行,完成。
六、運作C代碼
仿照第五步,建立helloworld.c檔案,鍵入或粘貼C語言的helloworld代碼
#include <stdio.h>
#include <windows.h>
int main() {
printf("hello world!\n");
system("pause");
return 0;
}
在.c頁面内單擊F5,稍候片刻出現同名.exe并自動執行,完成。
最後,感謝以下部落客的部落格進行參考
【VSCode】Windows下VSCode編譯調試c/c++【更新 2018.03.27】
在Windows中安裝MinGW-w64