1. 背景
??平常都是使用Visual Studio來編譯、調(diào)試和運(yùn)行C/C++程序,感覺其編譯調(diào)試運(yùn)行環(huán)境還是不錯(cuò)的,但就是體量過大,每次啟動(dòng)都要花較長時(shí)間。源程序才幾十行,VS項(xiàng)目的大小就幾十M,而且新建項(xiàng)目比較費(fèi)時(shí)。故現(xiàn)在嘗試使用Visual Studio Code來搭建C/C++編譯調(diào)試運(yùn)行環(huán)境,以方便對小型的C/C++程序進(jìn)行編程。
VS和VS Code
2. 搭建步驟
2.1 下載安裝VS Code和Mingw-w64。
VS Code官方下載(https://code.visualstudio.com/Download)
mingw-w64官方下載(https://sourceforge.net/projects/mingw-w64/files/mingw-w64/)
VS Code下載選項(xiàng)
mingw-w64下載選項(xiàng)
提示:
1. MinGW-W64-install.exe是安裝程序,下載后直接點(diǎn)擊安裝即可,不過要求你的網(wǎng)絡(luò)要好,不然下不了。
2. i686 的是32位版,x86_64 的是64位版;posix和win32是os接口類型;sjlj, seh, dwarf 是異常處理方案。根據(jù)電腦版本選擇 i686或x86_64,其它的推薦posix+sjlj,下載后直接解壓復(fù)制你想安裝的位置即可,注意路徑不能有空格或中文。
3. MinGW-W64需要配置環(huán)境。我的電腦>右鍵選擇屬性>點(diǎn)擊高級系統(tǒng)設(shè)置>點(diǎn)擊環(huán)境變量>點(diǎn)擊系統(tǒng)變量path>添加MinGW-W64的bin地址(C:\mingw64\bin;)。記得點(diǎn)擊確定保存設(shè)置,使用“gcc -v”測試MinGW-W64已裝好
系統(tǒng)變量path路徑
MinGW-W64安裝測試結(jié)果
2.2 安裝VS Code插件。
VS Code插件
插件 | 用途 |
---|---|
C/C++ | 搭建C/C++環(huán)境必需 |
C++ Intellisense | 智能提示插件支持部分的語法高亮,可選 |
Chinese (Simplified) Language Pack for Visual Studio Code | vs code漢化插件,可選 |
Code Runner | 運(yùn)行程序的快捷鍵插件,可選 |
Path Autocomplete | 路徑自動(dòng)提示插件,可選 |
3. 代碼調(diào)試
-
打開文件夾,新建1.cpp。
打開文件夾
新建1.cpp
#include <iostream>
int main()
{
int a = 9;
std::cout << a << std::endl;
std::cout << "hello" << std::endl;
std::cout << "你好" << std::endl;
return 0;
}
- 打開1.cpp,點(diǎn)擊“調(diào)試”>“啟動(dòng)調(diào)試”。選擇“C++(GDB/LLDB)”,選擇“g++.exe build and debug active file”,如此可以自動(dòng)創(chuàng)建launch.json。
啟動(dòng)調(diào)試
選擇C++(GDB/LLDB)
選擇g++.exe build and debug active file
{
// 使用 IntelliSense 了解相關(guān)屬性。
// 懸停以查看現(xiàn)有屬性的描述。
// 欲了解更多信息,請?jiān)L問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
- 打開1.cpp,點(diǎn)擊“調(diào)試”>“啟動(dòng)調(diào)試”(不要在launch.json上進(jìn)行調(diào)試)。此時(shí)系統(tǒng)報(bào)錯(cuò),點(diǎn)擊“配置任務(wù)” ,選擇“g++.exe build active file”。如此系統(tǒng)會(huì)自動(dòng)創(chuàng)建tasks.json。
若“g++.exe build active file”不存在,可能是因?yàn)槟阍趌aunch.json進(jìn)行調(diào)試,轉(zhuǎn)換到1.cpp重新調(diào)試即可。
系統(tǒng)報(bào)錯(cuò)
g++.exe build active file
{
// 有關(guān) tasks.json 格式的文檔,請參見
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
- 最后打開1.cpp,點(diǎn)擊“調(diào)試”>“啟動(dòng)調(diào)試”即可調(diào)試運(yùn)行。
調(diào)試結(jié)果
4. 代碼運(yùn)行(3種方法)
4.1 以非調(diào)試模式運(yùn)行(Ctrl + F5)
image.png
運(yùn)行結(jié)果
注意:其編碼方式是UTF-8。若換成GB2312,可能無法輸出中文。
GB2312無法輸出中文
4.2 終端g++命令運(yùn)行
g++ 1.cpp -o 1.exe; ./1.exe
終端
運(yùn)行結(jié)果
注意:其編碼方式是GB2312。若換成UTF-8,輸出結(jié)果中的中文會(huì)亂碼。
中文亂碼
4.3 安裝Code Runner插件,按運(yùn)行鍵(圖中紅色矩形)運(yùn)行
Code Runner
運(yùn)行結(jié)果
注意:其編碼方式是GB2312。若換成UTF-8,輸出結(jié)果中的中文會(huì)亂碼。
中文亂碼
5. 總結(jié)
- 熟悉VS Code不可能一蹴而就,搭建環(huán)境時(shí)只需把基本環(huán)境搭建好,能夠編譯調(diào)試運(yùn)行C/C++程序即可,不需一下添加過多功能或安裝許多插件。VS Code用多后有需求,自然而然就知道怎么配置環(huán)境,像快捷鍵之類都不怎么需要記,用多了自然就知道了。
- 網(wǎng)上有許多c_cpp_properties.json、tasks.json、launch.json,重點(diǎn)是看懂再用。對于新手而言,系統(tǒng)自動(dòng)生動(dòng)的tasks.json和launch.json已基本夠用。
- 對于VS Code中文亂碼有疑惑,可以參考VS Code:4個(gè)中文亂碼問題及解決方法。