vs code的安裝與配置
vs code的安裝與配置
Vscode是一個輕量級的編輯器,但是配置有點複雜,這裡我做了C/C++開發環境配置總結,適用於windows/Linux系統
整體步驟
安裝VSCode
在VSCode內安裝c++外掛
安裝編譯除錯環境
修改VSCode除錯配置檔案
安裝VsCode
下載地址:https://code.visualstudio.com/
注意:建議直接安裝在C盤,方便配置,軟體大小本身不大!!!
注意:一定要將 新增到PATH 這項打鉤!!!
安裝C++外掛
開啟VScode,在左側一欄,最下面一個圖示是擴充套件商店,或者快捷鍵ctrl+shift+x直接切換到商店介面,然後輸入C++,然後點選安裝,安裝之後重啟生效,安裝其他外掛也是在這裡搜尋安裝
在這裡插入圖片描述
這裡我推薦幾個我學習碼程式碼常用的外掛:
-
名稱: Chinese (Simplified) Language 中文介面配置外掛,英語大神請忽略
-
名稱: vscode-icons 圖示外掛可以使得檔案結構更加清晰
- 名稱: Bracket Pair Colorizer 括號高亮,彩虹括號
- 名稱: One Dark Pro 一個非常好看的黑色介面主題外掛
- 名稱: Code Runner 程式碼執行外掛,右鍵即可編譯執行單檔案,很方便
安裝編譯除錯環境
Windows系統
安裝 mingw-w64
目前windows下除錯僅支援 Cygwin 和 MinGW,mingw-w64 ,本文這裡使用mingw-w64.
mingw-w64下載地址:
選擇圖中紅色標記,點選會自動跳轉下載頁面下載
下載完後,直接將 mingw-w64 解壓到一個合適的目錄,比如我解壓C:\Program Files下。推薦放C盤,路徑好找,只有600M左右,不影響。
配置環境變數
找到解壓的目錄,複製解壓出來的bin目錄的地址
右擊此電腦選擇屬性
依次點選 高階系統設定 – 環境變數
點選Path,選擇編輯,使用者變數和系統變數兩個Path都要改哦
新建,將複製的目錄位置貼上到此,確定
在桌面上按住shift點選滑鼠右鍵-在此處開啟powershell, 輸入 gcc -v, 看到如下介面說明環境變數配置成功
或者檢查 win+R cmd 輸入 gcc -v,看到如下介面說明環境變數配置成功
配置VS Code除錯環境
建議配置之前新建一個目錄專門用來儲存C/CPP檔案,因為配置設定VS Code會預設儲存到配置時檔案的目錄下。我這裡建立有一個 *CPP *資料夾
開啟CPP資料夾,新建 .vscode資料夾並開啟
在 .vscode 資料夾中,依次新建3個檔案,名稱如下:
**c_cpp_properties.json**
**launch.json**
**tasks.json**
配置這3個檔案
launch.json 檔案如下圖配置
“miDebuggerPath”:後面修改為你的 mingw64 路徑
“preLaunchTask”: “task g++” 和 tasks.json 中的 “label”: “task g++” 引號中的名字要一致,本程式碼中為 task g++
c++
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",//配置型別,只能為cppdbg
"request": "launch",//請求配置型別,可以為launch(啟動)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",//除錯程式的路徑名稱
"args": [],//除錯傳遞引數
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,//true顯示外接的控制檯視窗,false顯示內建終端
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++",//除錯前執行的任務,就是之前配置的tasks.json中的label欄位
}
]
}
tasks.json 檔案如下圖配置
c++
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",//任務的名字,就是剛才在命令面板中選擇的時候所看到的,可以自己設定
"command": "g++",
"args": [//編譯時候的引數
"-g",//新增gdb除錯選項
"${file}",
"-o",//指定生成可執行檔案的名稱
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true//表示快捷鍵Ctrl+Shift+B可以執行該任務
}
}
]
}
c_cpp_properties.json 檔案如下圖配置
Code
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
配置完上面3項,你的vscode應該就可以正常除錯了
最後幾點注意:
【1】程式檔案路徑不要有 中文,否則可能會導致 除錯失敗
【2】vscode設定保留在單資料夾中,你如果要更換資料夾,請把 .vscode 資料夾也複製到你要更改的資料夾中
Linux系統
安裝 gdb 除錯環境
開啟終端,在root使用者許可權下:輸入以下命令
apt-get update
apt-get install gdb
如圖,按提示操作即可,等待程式執行完成就安裝好gdb了
參考上面windows教程,安裝 vscode,安裝拓展外掛,新建資料夾和4個配置檔案。
參考命令:
deb包安裝方式步驟:
1、找到相應的軟體包,比如soft.version.deb,下載到本機某個目錄;
2、開啟一個終端,su -成root使用者;
3、cd soft.version.deb所在的目錄;
4、輸入dpkg -i soft.version.deb
配置這4個檔案
launch.json 檔案如下圖配置
“miDebuggerPath”:後面修改為你的 gdb 路徑
“preLaunchTask”: “build” 和 tasks.json 中的 “label”: “build” 引號中的名字要一致,本程式碼中為 build
c++
{
"version": "0.2.0",
"configurations": [
{
"name": "debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json 檔案如下圖配置
c++
{
// 有關 tasks.json 格式的文件,請參見
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out",
"-l",
"pthread"
],
"presentation": {
"reveal": "never"
},
"problemMatcher": [
"$gcc"
]
}
]
}
c_cpp_properties.json 檔案如下圖配置
c++
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
settings.json檔案如下圖配置
c++
{
"files.associations": {
"iostream": "cpp",
"stdio.h": "c",
"ostream": "cpp",
"array": "cpp"
}
}
配置完上面4項,你的vscode應該就可以正常除錯了
最後幾點注意:
【1】程式檔案路徑不要有中文,否則會導致==除錯失敗
【2】vscode設定保留在單資料夾中,你如果要更換資料夾,請把 .vscode 資料夾也複製到你要更改的資料夾中
【3】中文顯示亂碼問題 VS code 編碼設定/檔案亂碼
點選檔案->首選項->設定,然後在右邊使用者設定輸入:
“files.autoGuessEncoding”: true,
插入這一行內用並儲存。
或者如下圖打鉤:
入這個設定後,VSCode會在開啟檔案時嘗試猜測字符集編碼。
【4】VSCode內部終端執行程式碼
設定在終端執行 : 首選項 -> 設定 -> 擴充套件 -> Run Code ,勾選 ”Run In Terminal“ 和 ”Save File Before Run”
這樣的話 寫個demo執行一下吧