2020-06-14
首先,介紹自己電腦:Ubuntu18.04、VS Code 1.46版
本文目的:爲VS Code配置好C++ 開發環境,以及VS Code +CMake的配置
對於C++ 工程,有四個必要的json
配置文件,先ctrl+shift+p打開輸入指令分別是:
c_cpp_properties.json
:配置項目結構,自動生成和更新,輸入C/C++:Edit configurationtask.json
: 構建和編譯運行項目,輸入Task:Configure Task,模板,Otherslaunch.json
: 調試,讀取可執行文件setting.json
: 輸入setting
針對兩種情況分別進行介紹,最後根據十四講中使用Eigen進行實驗。
一、VS Code 的C++開發環境
摘要:
1.新建C/C++工程,VScode以文件夾爲管理工程的方式,因此需要建立一個文件夾來保存工程。
2.配置launch.json
文件,讀取可執行文件
。需要進行修改地方的是指定運行的文件,其次我們還可以在裏面添加build任務,用於調試
。
3.配置tasks.json
文件,這個文件用來方便用戶自定義任務,我們可以通過這個文件來添加g++/gcc或者是make命令,方便我們編譯程序
。
4.之後就可以進行基礎的C/C++開發與調試了。
1、建立工程
新建一個工作區文件夾,然後在VScode中打開這個文件夾。VScode調試必須在工作區文件夾下,單獨打開一個文件調試會報錯。VScode不支持中文路徑,文件夾名稱不能有空格。
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World"<<endl;
getchar();
return 0;
}
2、更改配置文件(launch.json)
launch.json目的:讀取執行out文件
點擊左側的Debug按鈕,選擇添加配置(Add
configuration),然後選擇C++(GDB/LLDB),然後點擊默認生成,將自動生成launch.json文件,具體操作如下:
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 啓動",// 配置名稱
"type": "cppdbg",// 配置類型
"request": "launch",// 請求配置類型,launch或者attach
"program": "輸入程序名稱,例如 ${workspaceFolder}/a.out",// 進行調試程序的路徑,程序生成文件.out
"args": [],// 傳遞給程序的命令行參數,一般爲空
"stopAtEntry": false,// 調試器是否在目標的入口點停止,
"cwd": "${workspaceFolder}",// 項目目錄
"environment": [],
"externalConsole": false,// 調試時是否顯示控制檯窗口,一般爲true顯示控制檯
"MIMode": "gdb",// 指定連接的調試器
"setupCommands": [
{
"description": "爲 gdb 啓用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
更改:
將program內容改爲調試時運行的程序。
"program": "輸入程序名稱,例如 ${workspaceFolder}/a.out"
改爲
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out"
新增,preLaunchTask 使得每次調試之前會自動進行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、更改編譯任務(task.json)
task.json:定義編譯
方法,轉爲計算機可識別的語言,生成out文件
。
快捷鍵ctrl+shift+p打開命令行,輸入:
Task:Configure Task
使用模版創建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",// 任務名
"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、斷點調試
以上工作完成後即可編譯運行C/C++程序。不過在調試之前最好先CTRL+SHIFT+B
編譯一下,選擇執行我們的build任務,build成功後,點擊開始調試。
二、CMake調試C++ 工程
1、創建文件
在文件夾內創建文件
~$ 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)
# 可執行程序 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”)
開啓debug 不然斷點調試是無效的
2、開始調試
首先要build生成可執行文件result,有了可執行文件才能進行debug操作,然後再設置斷點,按下F5,進行調試。
在圖中最左側第四個小蜘蛛形狀的圖標(調試),點擊左上方的小齒輪,添加配置(C++GDB/LLDB),修改launch.json文件爲:
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 啓動",
"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",// 更改
是爲了生成的可執行文件result到build文件夾內。
之後按下最下方的Build按鍵,生成可執行文件。
接下來設置斷點,按下F5,進行調試
3、配置 C++ IntelliSense
Ctrl+shift+p打開命令選項,選擇C/C++:Edit configuration ,自動生成 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的引用和庫的路徑,根據引用內容進行配置。
三、實例分析
打開《視覺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 模塊提供了各種旋轉和平移的表示
// 3D 旋轉矩陣直接使用 Matrix3d 或 Matrix3f
Eigen::Matrix3d rotation_matrix = Eigen::Matrix3d::Identity();
// 旋轉向量使用 AngleAxis, 它底層不直接是Matrix,但運算可以當作矩陣(因爲重載了運算符)
Eigen::AngleAxisd rotation_vector ( M_PI/4, Eigen::Vector3d ( 0,0,1 ) ); //沿 Z 軸旋轉 45 度
cout .precision(3);
cout<<"rotation matrix =\n"<<rotation_vector.matrix() <<endl; //用matrix()轉換成矩陣
// 也可以直接賦值
rotation_matrix = rotation_vector.toRotationMatrix();
// 用 AngleAxis 可以進行座標變換
Eigen::Vector3d v ( 1,0,0 );
Eigen::Vector3d v_rotated = rotation_vector * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
// 或者用旋轉矩陣
v_rotated = rotation_matrix * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
// 歐拉角: 可以將旋轉矩陣直接轉換成歐拉角
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(); // 雖然稱爲3d,實質上是4*4的矩陣
T.rotate ( rotation_vector ); // 按照rotation_vector進行旋轉
T.pretranslate ( Eigen::Vector3d ( 1,3,4 ) ); // 把平移向量設成(1,3,4)
cout << "Transform matrix = \n" << T.matrix() <<endl;
// 用變換矩陣進行座標變換
Eigen::Vector3d v_transformed = T*v; // 相當於R*v+t
cout<<"v tranformed = "<<v_transformed.transpose()<<endl;
// 對於仿射和射影變換,使用 Eigen::Affine3d 和 Eigen::Projective3d 即可,略
// 四元數
// 可以直接把AngleAxis賦值給四元數,反之亦然
Eigen::Quaterniond q = Eigen::Quaterniond ( rotation_vector );
cout<<"quaternion = \n"<<q.coeffs() <<endl; // 請注意coeffs的順序是(x,y,z,w),w爲實部,前三者爲虛部
// 也可以把旋轉矩陣賦給它
q = Eigen::Quaterniond ( rotation_matrix );
cout<<"quaternion = \n"<<q.coeffs() <<endl;
// 使用四元數旋轉一個向量,使用重載的乘法即可
v_rotated = q*v; // 注意數學上是qvq^{-1}
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
return 0;
}
launch.json配置爲:
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 啓動",
"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",//編譯的項目名,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"// 更改
}
],
"version": 4
}
按下build生成可執行文件eigenGeometry
生成可執行文件後,按下F5,進行調試
參考:
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