最近迷上了vscode這款編譯器,小巧美觀,用起來也很順手,最主要的是全平臺,正好最近要上手作Linux C客戶端,之前沒接觸過linux,先拿它先在WIN上練練手。html
這幾天在網上找了很是多的教程,win老是配很差環境。(linux和win一塊兒開始配的,感受linux環境比win好配多了,下一篇博文再把linux端的配置過程分享出來,這裏先寫win的)。linux
環境:c++
WIN10 64 專業版shell
vscode版本:1.24.1json
launch.json版本:0.2.0windows
tasks.json版本:2.0.0ui
mingw-w64版本:8.1.0spa
過程:.net
1、 安裝vscode插件
vscode官網下載安裝包直接安裝便可
2、 vscode內安裝C/C++ 插件
vscode內按快捷組合鍵Ctrl+Shift+X(或如圖第①步點擊[拓展]按鈕)打開拓展分頁,在搜索欄輸入" C ",查找到如圖的第一個插件,安裝並從新加載之。
3、 安裝mingw-w64(具體安裝與環境變量配置能夠查看這裏)
在mingw-w64官網下載64位的mingw-w64在線安裝包(以在線包爲例)或離線包(離線包直接解壓出來就能用)
在線包:根據系統選擇合適的安裝包進行下載,選擇在線安裝器
下載完成後出現以下安裝包
安裝該包,在Setting 界面將Architecture選項改成x86_64,其餘不變,選擇合適的安裝路徑(默認或從新指定均可以,路徑中不要有中文)
配置計算機環境變量如圖(個人安裝路徑是D:\mingw64,所以環境變量這麼加)
安裝完成後打開控制檯,分別輸入 g++ --version 和 gcc --version 查看環境是否安裝成功(是否有當前版本號)
4、重啓電腦(這裏看了其餘不少博主的沒有提到,我沒有重啓,後來vscode代碼寫出來跑了不少次提示沒有找到g++命令,最後重啓解決)
5、運行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文件,代碼自動執行,完成。
6、運行C代碼
仿照第五步,新建helloworld.c文件,鍵入或粘貼C語言的helloworld代碼
#include <stdio.h> #include <windows.h> int main() { printf("hello world!\n"); system("pause"); return 0; }
在.c頁面內單擊F5,稍候片刻出現同名.exe並自動執行,完成。
最後,感謝如下博主的博客進行參考