詳解Ubuntu18.04配置VSCode+CMake的C++開(kāi)發(fā)環(huán)境
首先,介紹自己電腦:Ubuntu18.04、VS Code 1.46版
本文目的:為VS Code配置好C++ 開(kāi)發(fā)環(huán)境,以及VS Code +CMake的配置
對(duì)于C++ 工程,有四個(gè)必要的json
配置文件,先ctrl+shift+p打開(kāi)輸入指令分別是:
c_cpp_properties.json
:配置項(xiàng)目結(jié)構(gòu),自動(dòng)生成和更新,輸入C/C++:Edit configurationtask.json
: 構(gòu)建和編譯運(yùn)行項(xiàng)目,輸入Task:Configure Task,模板,Otherslaunch.json
: 調(diào)試,讀取可執(zhí)行文件setting.json
: 輸入setting
針對(duì)兩種情況分別進(jìn)行介紹,最后根據(jù)十四講中使用Eigen進(jìn)行實(shí)驗(yàn)。
一、VS Code 的C++開(kāi)發(fā)環(huán)境
摘要:
1.新建C/C++工程,VScode以文件夾為管理工程的方式,因此需要建立一個(gè)文件夾來(lái)保存工程。
2.配置launch.json
文件,讀取可執(zhí)行文件
。需要進(jìn)行修改地方的是指定運(yùn)行的文件,其次我們還可以在里面添加build任務(wù),用于調(diào)試
。
3.配置tasks.json
文件,這個(gè)文件用來(lái)方便用戶(hù)自定義任務(wù),我們可以通過(guò)這個(gè)文件來(lái)添加g++/gcc或者是make命令,方便我們編譯程序
。
4.之后就可以進(jìn)行基礎(chǔ)的C/C++開(kāi)發(fā)與調(diào)試了。
1、建立工程
新建一個(gè)工作區(qū)文件夾,然后在VScode中打開(kāi)這個(gè)文件夾。VScode調(diào)試必須在工作區(qū)文件夾下,單獨(dú)打開(kāi)一個(gè)文件調(diào)試會(huì)報(bào)錯(cuò)。VScode不支持中文路徑,文件夾名稱(chēng)不能有空格。
#include <iostream> using namespace std; int main(){ cout<<"Hello World"<<endl; getchar(); return 0; }
2、更改配置文件(launch.json)
launch.json目的:讀取執(zhí)行out文件
點(diǎn)擊左側(cè)的Debug按鈕,選擇添加配置(Add
configuration),然后選擇C++(GDB/LLDB),然后點(diǎn)擊默認(rèn)生成,將自動(dòng)生成launch.json文件,具體操作如下:
{ // 使用 IntelliSense 了解相關(guān)屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 啟動(dòng)",// 配置名稱(chēng) "type": "cppdbg",// 配置類(lèi)型 "request": "launch",// 請(qǐng)求配置類(lèi)型,launch或者attach "program": "輸入程序名稱(chēng),例如 ${workspaceFolder}/a.out",// 進(jìn)行調(diào)試程序的路徑,程序生成文件.out "args": [],// 傳遞給程序的命令行參數(shù),一般為空 "stopAtEntry": false,// 調(diào)試器是否在目標(biāo)的入口點(diǎn)停止, "cwd": "${workspaceFolder}",// 項(xiàng)目目錄 "environment": [], "externalConsole": false,// 調(diào)試時(shí)是否顯示控制臺(tái)窗口,一般為true顯示控制臺(tái) "MIMode": "gdb",// 指定連接的調(diào)試器 "setupCommands": [ { "description": "為 gdb 啟用整齊打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
更改:
將program內(nèi)容改為調(diào)試時(shí)運(yùn)行的程序。
"program": "輸入程序名稱(chēng),例如 ${workspaceFolder}/a.out"
改為
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out"
新增,preLaunchTask 使得每次調(diào)試之前會(huì)自動(dòng)進(jìn)行build:
"preLaunchTask": "build",
最終版本為:
{ // 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", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "preLaunchTask": "build", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
3、更改編譯任務(wù)(task.json)
task.json:定義編譯
方法,轉(zhuǎn)為計(jì)算機(jī)可識(shí)別的語(yǔ)言,生成out文件
。
快捷鍵ctrl+shift+p打開(kāi)命令行,輸入:
Task:Configure Task
使用模版創(chuàng)建Tasks.json文件 →
Others:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "echo",// 任務(wù)名 "type": "shell", "command": "echo Hello" // 指令 } ] }
更改為:
{ // 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": "g++", "args": ["-g", "${file}", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"] } ] }
4、斷點(diǎn)調(diào)試
以上工作完成后即可編譯運(yùn)行C/C++程序。不過(guò)在調(diào)試之前最好先CTRL+SHIFT+B
編譯一下,選擇執(zhí)行我們的build任務(wù),build成功后,點(diǎn)擊開(kāi)始調(diào)試。
二、CMake調(diào)試C++ 工程
1、創(chuàng)建文件
在文件夾內(nèi)創(chuàng)建文件
~$ touch main.cpp ~$ touch CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) # 工程vscode_cmake project(vscode_cmake) #dubug 模式 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") set(SRC_LIST main.cpp) # 可執(zhí)行程序 result add_executable(result ${SRC_LIST})
main.cpp
#include<iostream> using namespace std; int main(){ int a = 2+3; int b = a+3; for(int i = 0; i<10; i++){ cout<<"hello vs code & cmake..."<<endl; } return 0; }
其中, 需要在CMakeLists.txt 里加
set (CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -g”)
開(kāi)啟debug 不然斷點(diǎn)調(diào)試是無(wú)效的
2、開(kāi)始調(diào)試
首先要build生成可執(zhí)行文件result,有了可執(zhí)行文件才能進(jìn)行debug操作,然后再設(shè)置斷點(diǎn),按下F5,進(jìn)行調(diào)試。
在圖中最左側(cè)第四個(gè)小蜘蛛形狀的圖標(biāo)(調(diào)試),點(diǎn)擊左上方的小齒輪,添加配置(C++GDB/LLDB),修改launch.json文件為:
{ // 使用 IntelliSense 了解相關(guān)屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 啟動(dòng)", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/result",// 更改 "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "為 gdb 啟用整齊打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
更改了
"program": "${workspaceFolder}/build/result",// 更改
是為了生成的可執(zhí)行文件result到build文件夾內(nèi)。
之后按下最下方的Build按鍵,生成可執(zhí)行文件。
接下來(lái)設(shè)置斷點(diǎn),按下F5,進(jìn)行調(diào)試
3、配置 C++ IntelliSense
Ctrl+shift+p打開(kāi)命令選項(xiàng),選擇C/C++:Edit configuration ,自動(dòng)生成 c_cpp_properties.json配置文件。
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**" ], "defines": [], "compilerPath": "/usr/bin/clang", "cStandard": "c11", "cppStandard": "c++14", "intelliSenseMode": "clang-x64", "configurationProvider": "ms-vscode.cmake-tools" } ], "version": 4 }
最主要的事includePath的引用和庫(kù)的路徑,根據(jù)引用內(nèi)容進(jìn)行配置。
三、實(shí)例分析
打開(kāi)《視覺(jué)SLAM十四講》的ch3的useGeometry文件夾
CmakeLists.txt:
cmake_minimum_required( VERSION 2.8 ) project( geometry ) # 添加Eigen頭文件 include_directories( "/usr/include/eigen3" ) add_executable( eigenGeometry eigenGeometry.cpp )
eigenGeometry.cpp:
#include <iostream> #include <cmath> using namespace std; #include <Eigen/Core> // Eigen 幾何模塊 #include <Eigen/Geometry> /**************************** * 本程序演示了 Eigen 幾何模塊的使用方法 ****************************/ int main ( int argc, char** argv ) { // Eigen/Geometry 模塊提供了各種旋轉(zhuǎn)和平移的表示 // 3D 旋轉(zhuǎn)矩陣直接使用 Matrix3d 或 Matrix3f Eigen::Matrix3d rotation_matrix = Eigen::Matrix3d::Identity(); // 旋轉(zhuǎn)向量使用 AngleAxis, 它底層不直接是Matrix,但運(yùn)算可以當(dāng)作矩陣(因?yàn)橹剌d了運(yùn)算符) Eigen::AngleAxisd rotation_vector ( M_PI/4, Eigen::Vector3d ( 0,0,1 ) ); //沿 Z 軸旋轉(zhuǎn) 45 度 cout .precision(3); cout<<"rotation matrix =\n"<<rotation_vector.matrix() <<endl; //用matrix()轉(zhuǎn)換成矩陣 // 也可以直接賦值 rotation_matrix = rotation_vector.toRotationMatrix(); // 用 AngleAxis 可以進(jìn)行坐標(biāo)變換 Eigen::Vector3d v ( 1,0,0 ); Eigen::Vector3d v_rotated = rotation_vector * v; cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl; // 或者用旋轉(zhuǎn)矩陣 v_rotated = rotation_matrix * v; cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl; // 歐拉角: 可以將旋轉(zhuǎn)矩陣直接轉(zhuǎn)換成歐拉角 Eigen::Vector3d euler_angles = rotation_matrix.eulerAngles ( 2,1,0 ); // ZYX順序,即roll pitch yaw順序 cout<<"yaw pitch roll = "<<euler_angles.transpose()<<endl; // 歐氏變換矩陣使用 Eigen::Isometry Eigen::Isometry3d T=Eigen::Isometry3d::Identity(); // 雖然稱(chēng)為3d,實(shí)質(zhì)上是4*4的矩陣 T.rotate ( rotation_vector ); // 按照rotation_vector進(jìn)行旋轉(zhuǎn) T.pretranslate ( Eigen::Vector3d ( 1,3,4 ) ); // 把平移向量設(shè)成(1,3,4) cout << "Transform matrix = \n" << T.matrix() <<endl; // 用變換矩陣進(jìn)行坐標(biāo)變換 Eigen::Vector3d v_transformed = T*v; // 相當(dāng)于R*v+t cout<<"v tranformed = "<<v_transformed.transpose()<<endl; // 對(duì)于仿射和射影變換,使用 Eigen::Affine3d 和 Eigen::Projective3d 即可,略 // 四元數(shù) // 可以直接把AngleAxis賦值給四元數(shù),反之亦然 Eigen::Quaterniond q = Eigen::Quaterniond ( rotation_vector ); cout<<"quaternion = \n"<<q.coeffs() <<endl; // 請(qǐng)注意coeffs的順序是(x,y,z,w),w為實(shí)部,前三者為虛部 // 也可以把旋轉(zhuǎn)矩陣賦給它 q = Eigen::Quaterniond ( rotation_matrix ); cout<<"quaternion = \n"<<q.coeffs() <<endl; // 使用四元數(shù)旋轉(zhuǎn)一個(gè)向量,使用重載的乘法即可 v_rotated = q*v; // 注意數(shù)學(xué)上是qvq^{-1} cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl; return 0; }
launch.json配置為:
{ // 使用 IntelliSense 了解相關(guān)屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 啟動(dòng)", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/eigenGeometry",// 更改 "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "為 gdb 啟用整齊打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
task.json配置為:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "make build",//編譯的項(xiàng)目名,build,更改 "type": "shell", "command": "cd ./build ;cmake ../ ;make",//編譯命令,更改 "group": { "kind": "build", "isDefault": true } }, { "label": "clean", "type": "shell", "command": "make clean", } ] }
c_cpp_properties.json
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", // 更改 "/usr/include", "/usr/local/include" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64", "compileCommands": "${workspaceFolder}/build/compile_commands.json"http:// 更改 } ], "version": 4 }
按下build生成可執(zhí)行文件eigenGeometry
生成可執(zhí)行文件后,按下F5,進(jìn)行調(diào)試
參考:
https://blog.csdn.net/weixin_43374723/article/details/84064644
https://blog.csdn.net/zzz_xxj/article/details/86568353
https://blog.csdn.net/wanzew/article/details/83097457
https://blog.csdn.net/orange_littlegirl/article/details/88397361
到此這篇關(guān)于詳解Ubuntu18.04配置VSCode+CMake的C++開(kāi)發(fā)環(huán)境的文章就介紹到這了,更多相關(guān)VSCode CMake配置C++開(kāi)發(fā)環(huán)境內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言項(xiàng)目小學(xué)生數(shù)學(xué)考試系統(tǒng)參考
今天小編就為大家分享一篇關(guān)于C語(yǔ)言項(xiàng)目小學(xué)生數(shù)學(xué)考試系統(tǒng)參考,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Linux下使用C/C++進(jìn)行UDP網(wǎng)絡(luò)編程詳解
UDP 是User Datagram Protocol 的簡(jiǎn)稱(chēng),中文名是用戶(hù)數(shù)據(jù)報(bào)協(xié)議,是一種無(wú)連接、不可靠的協(xié)議,本文主要介紹了如何在Linux下使用C/C++進(jìn)行UDP網(wǎng)絡(luò)編程,有需要的可以了解下2024-10-10C++中范圍(Ranges)與視圖(Views)的常見(jiàn)問(wèn)題、易錯(cuò)點(diǎn)
ranges和views是C20引入的重要特性,它們讓代碼更加簡(jiǎn)潔、高效且富有表達(dá)力,通過(guò)理解其基本概念、注意常見(jiàn)的陷阱,并合理應(yīng)用高級(jí)技巧,開(kāi)發(fā)者可以充分利用這些新特性,提升軟件質(zhì)量和開(kāi)發(fā)效率,,本文將深入淺出地探討ranges與views的基礎(chǔ)概念、常見(jiàn)問(wèn)題、易錯(cuò)點(diǎn)及避免策略2024-06-06C++項(xiàng)目開(kāi)發(fā)實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++項(xiàng)目開(kāi)發(fā)實(shí)現(xiàn)圖書(shū)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C++獲取本機(jī)登陸過(guò)的QQ號(hào)碼示例程序
這篇文章主要介紹了使用C++獲取本機(jī)登陸過(guò)的QQ號(hào)碼列表的程序示例,大家可以參考使用2013-11-11