準(zhǔn)備工作
從官網(wǎng)下載安裝 VS Code: https://code.visualstudio.com/
-
VS Code 本身只是一個(gè)代碼編輯器, 需要手動(dòng)添加編譯器扔字、調(diào)試器
- 這里選擇MSVC(cl, cdb)和MinGW(gcc/g++, gdb)
- MSVC可以通過(guò)VS安裝器安裝忧陪,MinGW從官網(wǎng)對(duì)應(yīng)Host下載安裝 MinGW: http://mingw-w64.org/doku.php/download(threads考慮跨平臺(tái)選擇posix標(biāo)準(zhǔn))
- 安裝完以后將MinGW和MSVC的目錄的
bin
文件夾添加到環(huán)境變量 Path 路徑
( CMD 輸入g++ -v
和cl
驗(yàn)證是否安裝完成)
C++項(xiàng)目創(chuàng)建
- 手動(dòng)創(chuàng)建一個(gè)工程目錄, 例如
MyProjects
, 用來(lái)存放代碼 - 用VS Code打開該工程目錄進(jìn)行配置和coding
- 推薦用這種方式來(lái)創(chuàng)建一個(gè)工程,方便識(shí)別
- 在當(dāng)前工程目錄下創(chuàng)建一個(gè)
.vscode
文件夾
共存兩種編譯方式
- 首先是創(chuàng)建編譯任務(wù)配置文件
tasks.json
和MSVC
,MinGW
文件夾,分別包含用cl和g++編譯兩個(gè)任務(wù)以及存放相關(guān)文件, 主要參數(shù)是args
編譯選項(xiàng):
tasks.json
{
"tasks": [
{
"type": "shell",
"label": "cl.exe build",
"command": "cl.exe",
"args": [
//頭文件路徑
"/I'D:\\Programs\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\include'",
"/I'C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\shared'",
"/I'C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\ucrt'",
"/I'C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\um'",
"${file}",
"/Zi", //生成調(diào)試信息
"/EHsc", //打開異常處理
"/Fo:${fileDirname}\\MSVC\\${fileBasenameNoExtension}.obj", //目標(biāo)文件
"/Fe:${fileDirname}\\MSVC\\${fileBasenameNoExtension}.exe", //可執(zhí)行文件
"/link", //鏈接標(biāo)志
//庫(kù)文件路徑
"/libpath:'D:\\Programs\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\lib\\x64'",
"/libpath:'C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\ucrt\\x64'",
"/libpath:'C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\um\\x64'"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always"
},
"problemMatcher": "$msCompile"
},
{
"type": "shell",
"label": "g++.exe build",
"command": "g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\MinGW\\${fileBasenameNoExtension}.exe",
//頭文件路徑
"-I D:\\Programs\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\x86_64-w64-mingw32\\include",
//庫(kù)文件路徑
"-L D:\\Programs\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\x86_64-w64-mingw32\\lib"
],
"options": {
"cwd": "D:\\Programs\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
]
}
],
"version": "2.0.0"
}
- 創(chuàng)建完成以后可以先單獨(dú)測(cè)試編譯, 置前當(dāng)前需要編譯的源文件(這個(gè)即為
${file}
), 然后點(diǎn)擊Terminal => Run Task,按照標(biāo)簽分別測(cè)試兩個(gè)compiler:
build.PNG
- 編譯沒(méi)問(wèn)題以后,下面就是配置啟動(dòng)和Debugger, 在
.vscode
中創(chuàng)建一個(gè)名為launch.json
的文件, 主要是通過(guò)type
來(lái)指定debugger和preLaunchTask
將之前的兩個(gè)編譯tasks作為前置任務(wù)先執(zhí)行(注意這里可以將externalConsole
設(shè)置為false
來(lái)創(chuàng)建一個(gè)console的子進(jìn)程避免打開額外的進(jìn)程窗口):
launch.json
{
"version": "0.2.0",
"configurations": [
//cdb
{
"name": "cl.exe build and debug",
"type": "cppvsdbg",
"request": "launch",
"program": "MSVC\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false, //子進(jìn)程console
"preLaunchTask": "cl.exe build",
"logging": {
"moduleLoad": false
},
},
//gdb
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\MinGW\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb.exe",
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build"
}
]
}
- 啟動(dòng)方法是選中錨點(diǎn)文件
${file}
, 例如這里的test.cpp
, 點(diǎn)擊左側(cè)的Run圖標(biāo),然后在左上角選擇相應(yīng)name的runner,cdb可以在下方的debug console查看信息,gdb直接在terminal查看:
runner.png
(后記:對(duì)于多源文件和方便性可以使用CMake,參考我的這一篇VS Code C++ CMake工具編譯,本文可以幫助復(fù)習(xí)一下編譯工具相關(guān)的知識(shí))