這篇文章的首要目的是,通過配置VSCode
,達(dá)到全平臺(tái)的一致C/C++開發(fā)體驗(yàn)。
對(duì)于編寫C/C++的環(huán)境,我們至少需要有文本編輯器、C/C++編譯器,最好還能有C/C++調(diào)試器。
VSCode
本質(zhì)上是一個(gè)文本編輯器,但是它有豐富的插件生態(tài),通過插件我們可以對(duì)C/C++程序進(jìn)行調(diào)試。而且,它擁有可自定義的任務(wù)系統(tǒng),通過任務(wù),可以封裝一些操作,化繁為簡。
如果談編譯器和調(diào)試器的話,一般來講,這兩個(gè)東西是成雙成對(duì)的,由gcc
和g++
編譯的程序,使用gdb
進(jìn)行調(diào)試,由clang
編譯的程序使用lldb
進(jìn)行調(diào)試,由msvc
編譯的程序用msvc
進(jìn)行調(diào)試。除了msvc
之外,其他的編譯器調(diào)試器都是可以跨平臺(tái)(Windows上通過Mingw
實(shí)現(xiàn))。 這幾種編譯器和調(diào)試器占據(jù)了大部分市場,所以實(shí)際上也沒得選,根據(jù)平臺(tái),Linux
下GNU
的gcc
、g++
和gdb
是主流,macOS
下,clang
+lldb
是主流,使用GNU
的也不能算少。Windows
平臺(tái)下比較復(fù)雜,主要有兩種,一種是通過Mingw
使用gcc
的,另一種則是使用微軟出品的宇宙最強(qiáng)IDEVisual Studio
。
上面提到全平臺(tái)的一致C/C++開發(fā)體驗(yàn),但是不同編譯器、調(diào)試器的使用方式并不相同。調(diào)試器的問題,VSCode
通過C/C++
插件已經(jīng)解決了。而編譯器的問題,需要一個(gè)通用的編譯構(gòu)建工具,通過CMake
生成Makefile
并編譯。
CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.
到這里,梳理一下需要用到的東西。
- 文本編輯器:Visual Studio Code。
- C/C++編譯器:gcc/g++(Linux)、clang(macOS)、msvc(Windows)。
- C/C++調(diào)試器:gdb(Linux)、lldb(macOS)、msvc(Windows)。
- 構(gòu)建工具:CMake、Make。
下面說明環(huán)境的安裝和配置步驟。
根據(jù)平臺(tái)下載安裝Visual Studio Code。
-
打開
VSCode
,搜索安裝擴(kuò)展C/C++
和CMake
。-
C/C++
。這個(gè)擴(kuò)展的作用是C/C++代碼提示和高亮、調(diào)試和代碼文件關(guān)聯(lián)跳轉(zhuǎn)。
vsccc.png -
CMake
。這個(gè)擴(kuò)展的作用是CMake語法提示和高亮。
vsccmake.png
-
-
安裝編譯器和調(diào)試器。
-
Windows
下載并安裝Visual Studio Community 。
安裝使用C++的桌面開發(fā)。
vscc.png -
Linux
-
Debian系列(Debian/Ubuntu)
sudo apt install gcc sudo apt install g++ sudo apt install gdb sudo apt install make
-
Redhat系列(RHEL/CentOS/Fedora)
sudo yum install gcc sudo yum install g++ sudo yum install gdb sudo yum install make
其它源碼安裝(自行搜索安裝方式)
-
-
macOS
xcode-select --install
-
-
安裝CMake。
Windows
如果安裝的Visual Studio Community
版本大于2017,并選擇了用于Windows的CMake工具,則不需要單獨(dú)安裝CMake
。-
Linux
-
Debian系列(Debian/Ubuntu)
sudo apt install cmake
-
Redhat系列(RHEL/CentOS/Fedora)
sudo yum install cmake
-
-
macOS
安裝CMake需要用到brew,首先安裝
brew
。/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install cmake
-
也可以選擇官網(wǎng)安裝
cmake.png
根據(jù)平臺(tái)下載并安裝相應(yīng)的安裝包安裝。
-
此時(shí),所需要的工具都已經(jīng)安裝完畢。
創(chuàng)建我們的C/C++項(xiàng)目文件夾,這里命名為CStart
,并用VSCode打開該文件夾。
新建下列文件結(jié)構(gòu)CStart │—— CMakeLists.txt │—— main.c |—— .vscode │—— │—— launch.json │—— │—— tasks.json
-
使用CMake構(gòu)建C/C++項(xiàng)目
CMakeLists.txt 項(xiàng)目cmake配置文件。關(guān)于CMakeLists.txt的編寫,展開來講有點(diǎn)長,可以到網(wǎng)上搜索相關(guān)教程。這是一個(gè)最簡單的版本。通過這個(gè)文件,統(tǒng)一全平臺(tái)下的項(xiàng)目管理與構(gòu)建配置。cmake_minimum_required (VERSION 2.8) #最低要求的CMake版本 project(CStart) # 項(xiàng)目名稱 file(GLOB SRC_FILE *.c) # 建立變量SRC_FILE為目錄下.c文件列表 add_executable (${PROJECT_NAME} ${SRC_FILE}) # 要求編譯可執(zhí)行文件
main.c 簡單的HelloWorld代碼
#include <stdio.h> int main(void) { printf("Hello world\n"); return 0; }
-
VSCode配置編譯任務(wù)與調(diào)試對(duì)象
在配置的時(shí)候會(huì)用到一些vscode的變量,用${}
包裹起來的那些。
${workspaceFolder}
是當(dāng)前工作空間(或vscode所打開根文件夾)在操作系統(tǒng)中絕對(duì)路徑
${workspaceFolderBasename}
是當(dāng)前工作空間(或vscode所打開根文件夾)的名稱tasks.json 這是
VSCode
任務(wù)的配置文件,通過配置它可以快速執(zhí)行各種命令。這里我們利用它來配置編譯構(gòu)建流程。我們要執(zhí)行的任務(wù)為建立build文件夾,在build文件夾中使用CMake生成并編譯。通過這個(gè)任務(wù)配置,統(tǒng)一全平臺(tái)下的程序編譯命令。{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { // 在根文件夾中執(zhí)行創(chuàng)建文件夾build的命令 // 除windows系統(tǒng)外執(zhí)行的命令為`mkdir -p build` // windows系統(tǒng)是在powershell中執(zhí)行命令`mkdir -Force build` "label": "build_dir", "command": "mkdir", "type": "shell", "args": [ "-p", "build" ], "windows": { "options": { "shell": { "executable": "powershell.exe" } }, "args": [ "-Force", "build" ], } }, { // 在build文件夾中調(diào)用cmake進(jìn)行項(xiàng)目配置 // 除windows系統(tǒng)外執(zhí)行的命令為`cmake -DCMAKE_BUILD_TYPE=<Debug|Release|RelWithDebInfo|MinSizeRel> ../` // windows系統(tǒng)是在visual stuido的環(huán)境中執(zhí)行命令`cmake -DCMAKE_BUILD_TYPE=<Debug|Release|RelWithDebInfo|MinSizeRel> ../ -G "CodeBlocks - NMake Makefiles"` "label": "cmake", "type": "shell", "command": "cmake", "args": [ "-DCMAKE_BUILD_TYPE=${input:CMAKE_BUILD_TYPE}", "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", // 生成compile_commands.json 供c/c++擴(kuò)展提示使用 "../" ], "options": { "cwd": "${workspaceFolder}/build", }, "windows": { "args": [ "-DCMAKE_BUILD_TYPE=${input:CMAKE_BUILD_TYPE}", "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", "../", "-G", "\"CodeBlocks - NMake Makefiles\"" ], "options": { "shell": { // "executable": "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\vcvarsall.bat", // 需要根據(jù)安裝的vs版本調(diào)用vs工具命令提示符 "executable": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat", "args": [ "${input:PLATFORM}", //指定平臺(tái) "-vcvars_ver=${input:vcvars_ver}", //指定vc環(huán)境版本 "&&" ] } }, }, "dependsOn": [ "build_dir" // 在task `build_dir` 后執(zhí)行該task ] }, { // 在build文件夾中調(diào)用cmake編譯構(gòu)建debug程序 // 執(zhí)行的命令為`cmake --build ./ --target all --` // windows系統(tǒng)如上需要在visual stuido的環(huán)境中執(zhí)行命令 "label": "build", "group": "build", "type": "shell", "command": "cmake", "args": [ "--build", "./", "--target", "all", "--" ], "options": { "cwd": "${workspaceFolder}/build", }, "problemMatcher": "$gcc", "windows": { "options": { "shell": { // "executable": "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\vcvarsall.bat", "executable": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat", "args": [ "${input:PLATFORM}", "-vcvars_ver=${input:vcvars_ver}", "&&" ] } }, "problemMatcher": "$msCompile" }, "dependsOn": [ "cmake" // 在task `cmake` 后執(zhí)行該task ] } ], "inputs": [ { "id": "CMAKE_BUILD_TYPE", "type": "pickString", "description": "What CMAKE_BUILD_TYPE do you want to create?", "options": [ "Debug", "Release", "RelWithDebInfo", "MinSizeRel", ], "default": "Debug" }, { "id": "PLATFORM", "type": "pickString", "description": "What PLATFORM do you want to create?", "options": [ "x86", "amd64", "arm", "x86_arm", "x86_amd64", "amd64_x86", "amd64_arm", ], "default": "amd64" }, { "id": "vcvars_ver", "type": "pickString", "description": "What vcvars_ver do you want to create?", "options": [ "14.2", // 2019 "14.1", // 2017 "14.0", // 2015 ], "default": "14.2" } ] }
launch.json 這是
VSCode
運(yùn)行調(diào)試的配置文件。全平臺(tái)統(tǒng)一的調(diào)試體驗(yàn)就靠它了。依賴于VSCode的C/C++擴(kuò)展。這里需要告訴VSCode你的C/C++程序在哪,以及運(yùn)行參數(shù),工作目錄等,用哪個(gè)調(diào)試器調(diào)試。{ // 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": "Launch Debug", //名稱 "type": "cppdbg", //調(diào)試類型,除使用msvc進(jìn)行調(diào)試外,均為該類型 "request": "launch", "program": "${workspaceFolder}/build/${workspaceFolderBasename}", //指定C/C++程序位置 "args": [], //指定運(yùn)行參數(shù) "stopAtEntry": false, "cwd": "${workspaceFolder}", //指定工作目錄 "preLaunchTask": "build", //在調(diào)試前會(huì)先調(diào)用build_debug這個(gè)task編譯構(gòu)建程序 "environment": [], "externalConsole": false, "osx": { //macOS的特定配置 // "miDebuggerPath": "/Applications/Xcode.app/Contents/ Developer/usr/bin/lldb-mi", //修改使用的lldb-mi,一般不需要修改 "MIMode": "lldb" //指定使用lldb進(jìn)行調(diào)試 }, "linux": { //linux的特定配置 "MIMode": "gdb", //指定使用gdb調(diào)試 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, "windows": { //windows的特定配置 "type": "cppvsdbg", //指定使用msvc進(jìn)行調(diào)試 "program": "${workspaceFolder}/build/${workspaceFolderBasename}.exe", //指定C/C++程序位置 } } ] }
-
加入斷點(diǎn),左鍵單擊即可,右鍵可以設(shè)置條件斷點(diǎn)等。
breakmain.png -
按下
F5
,VSCode
開始執(zhí)行launch.json
的命令,在執(zhí)行前會(huì)先運(yùn)行preLaunchTask
,編譯C代碼。
終端界面可以看到編譯的過程。
build.png
編譯完成后,文件目錄變成了這個(gè)樣子,生成了Makefile文件和可執(zhí)行文件還有調(diào)試信息,以及CMake緩存信息。
files.png
程序運(yùn)行到斷點(diǎn)處,如下圖。
break.png
左側(cè)為當(dāng)前的變量信息,還有上邊有調(diào)試的控制條,
|繼續(xù)|單步跳過|單步調(diào)試|單步跳出|重啟|停止。
切換到調(diào)試控制臺(tái)
界面,看到輸出了庫文件和調(diào)試信息加載過程。
debug.png
單擊調(diào)試控制條的繼續(xù)。輸出Hello World
,程序退出返回0。
hello .png 有時(shí)候我們只想編譯,不想運(yùn)行程序,可以單獨(dú)運(yùn)行
task: build_debug
。