需要用到的工具:
VSCode(Visual Studio Code)
一、首先打開官網鏈接,然后根據自己的電腦選擇合適的安裝程序進行下載。
二、在安裝時默認點擊下一步,最后記得勾選上添加path到系統變量里,方便鼠標右鍵直接用vscode打開相應文件。如圖:

C語言編譯包(MinGW-w64)
一、前面的工作做完后,然后是下載MinGW-w64編譯工具,這里不推薦下載上面綠色部分的在線安裝包,比較麻煩,直接下載下面的壓縮包,如圖所示:

下載完成后解壓文件,可以自行選擇目錄,我這里是選擇D盤下的軟件目錄。這里一定不要忘了添加環境變量(鼠標右鍵此電腦——屬性——高級系統設置——環境變量——系統變量path),需要進入bin和include文件夾並把他們的路徑添加到path里面。由於我是把mingw64安裝在D:\Users\data\software里面的,所以如圖所示:


環境變量配置好,就來驗證一下,打開win+R,輸入cmd命令,回車。第一步,輸入gcc -v,回車。第二步,輸入gdb,回車。若出現下面這些情況,說明你的環境變量已經沒有問題。


前面的工作已經完成,這里就開始下面的工作。
安裝vscode插件
這里推薦安裝C/C++以及C/C++ Compile Run插件。如圖:


安裝好之后就開始我們的編譯准備吧。
打開vscode的工作目錄,新建一個hello.c文件,在這里輸入:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("helloworld!");
return 0;
}
由於沒有進行vscode的C語言環境配置,所以會出現很多問題。這里就不一一闡述了,在工作目錄的下建立一個.vscode的文件夾用於存儲一些配置。由於是C語言,這里建立三個文件,分別是:c_cpp_properties.json,launch.json,tasks.json。具體內容如下:
// c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"D:/Users/data/software/mingw64**",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"D:/Users/data/software/mingw64/include/**",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/Users/data/software/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
]
}
}
],
"version": 4
}
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "cmd",
"preLaunchTask": "echo",
"args": [
"/C",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"&",
"echo.",
"&",
"pause"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole":true
},
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Users\\data\\software\\mingw64\\bin\\gdb.exe",// 自己電腦的gdb
"preLaunchTask": "echo",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
// tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"//解決中文亂碼
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
其中,通過Ctrl+H快捷鍵,把D:/Users/data/software/mingw64替換成你自己的mingw64安裝路徑即可。好了,現在就能成功進行編譯了。由於我這里在C/C++ Compile Run插件里面設置了Run in a new external terminal,所以會彈出新的cmd窗口。

在我們剛剛寫的c文件頁面,按F6即可運行,運行結果如下:

另外,為了節省大家的時間,附上我打包的vscode以及mingw64和.vscode的地址,有問題歡迎在下方留言!
