最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Ubuntu18.04配置VSCode+CMake的C++開(kāi)發(fā)環(huán)境

 更新時(shí)間:2021年03月12日 11:36:57   作者:try_again_later  
這篇文章主要介紹了詳解Ubuntu18.04配置VSCode+CMake的C++開(kāi)發(fā)環(huán)境,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

首先,介紹自己電腦: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 configuration
  • task.json: 構(gòu)建和編譯運(yùn)行項(xiàng)目,輸入Task:Configure Task,模板,Others
  • launch.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ǔ)言合并兩個(gè)帶頭節(jié)點(diǎn)升序排列鏈表

    C語(yǔ)言合并兩個(gè)帶頭節(jié)點(diǎn)升序排列鏈表

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言合并兩個(gè)帶頭節(jié)點(diǎn)升序排列鏈表的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 解決C語(yǔ)言輸入單個(gè)字符屏蔽回車(chē)符的問(wèn)題

    解決C語(yǔ)言輸入單個(gè)字符屏蔽回車(chē)符的問(wèn)題

    這篇文章主要介紹了解決C語(yǔ)言輸入單個(gè)字符屏蔽回車(chē)符的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • C++之Qt5雙緩沖機(jī)制案例教程

    C++之Qt5雙緩沖機(jī)制案例教程

    這篇文章主要介紹了C++之Qt5雙緩沖機(jī)制案例教程,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • gdb調(diào)試的簡(jiǎn)單操作總結(jié)

    gdb調(diào)試的簡(jiǎn)單操作總結(jié)

    gdb是一個(gè)命令行下的、功能強(qiáng)大的調(diào)試器,這篇文章主要為大家詳細(xì)介紹了gdb?調(diào)試的一些簡(jiǎn)單操作,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-08-08
  • strtok函數(shù)的使用示例

    strtok函數(shù)的使用示例

    今天小編就為大家分享一篇關(guān)于strtok函數(shù)的使用示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • 一篇文章帶你入門(mén)C語(yǔ)言:數(shù)組

    一篇文章帶你入門(mén)C語(yǔ)言:數(shù)組

    這篇文章主要介紹了C語(yǔ)言中數(shù)組的一些基本知識(shí)小結(jié),其中重點(diǎn)是對(duì)于數(shù)組的內(nèi)存分配相關(guān)方面的知識(shí)整理,需要的朋友可以參考下
    2021-08-08
  • C++智能指針讀書(shū)筆記

    C++智能指針讀書(shū)筆記

    本篇隨筆僅作為個(gè)人學(xué)習(xí)《C++ Primer》智能指針一節(jié)后的部分小結(jié),抄書(shū)嚴(yán)重,伴隨個(gè)人理解。主要介紹shared_ptr、make_shared、weak_ptr的用法和聯(lián)系
    2015-11-11
  • C++實(shí)現(xiàn)二分法求連續(xù)一元函數(shù)根

    C++實(shí)現(xiàn)二分法求連續(xù)一元函數(shù)根

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)二分法求連續(xù)一元函數(shù)根,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C++實(shí)現(xiàn)Dijkstra(迪杰斯特拉)算法

    C++實(shí)現(xiàn)Dijkstra(迪杰斯特拉)算法

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)Dijkstra(迪杰斯特拉)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • C++實(shí)現(xiàn)求動(dòng)態(tài)矩陣各元素的和

    C++實(shí)現(xiàn)求動(dòng)態(tài)矩陣各元素的和

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)求動(dòng)態(tài)矩陣各元素的和,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10

最新評(píng)論

思南县| 芦山县| 潜江市| 秦皇岛市| 周口市| 三原县| 甘南县| 广安市| 阆中市| 西城区| 桐梓县| 天津市| 四平市| 宣恩县| 怀远县| 德钦县| 琼中| 福海县| 沁源县| 东至县| 任丘市| 井研县| 遵化市| 鹿邑县| 柳州市| 武夷山市| 芒康县| 连平县| 额敏县| 和平区| 日照市| 黄大仙区| 宣威市| 新河县| 桐柏县| 昆明市| 昭觉县| 彭山县| 合水县| 盐池县| 诏安县|