Linux 下文件顏色說明
- 紅色表示壓縮包文件
- 綠色代表可執行文件
- 藍色表示文件夾
- 白色表示一般性文件
Linux 下hello SLAM 工程編譯
參考視覺SLAM工程,設計最簡單的文件如下:
#include<iostream>
using namespace std;
int main(int argc, char** argv)
{
cout<<"Hello, SLAM!"<<endl;
return 0;
}
g++ hello.cpp
則默認編譯生成a.out
,若要指定文件名,則採用g++ -o hello.out hello.cpp
在實際操作過程中,則採用cmake來管理代碼,此方法管理多文件時比較方便。首先創建build
文件夾就用來存儲生成的中間文件,然後創建並編輯CMakeLists.txt
文件如下:
#聲明Cmake的最低版本信息
cmake_minimum_required( VERSION 2.8)
#聲明一個cmake工程
project( HelloSLAM)
#添加執行文件
add_executable(HelloSLAM hello.cpp)
編譯過程中,先進入build文件夾,然後
cd build
cmake ..
make
工程中使用庫
首先編寫庫文件,這裏創建libHelloSlam.cpp,以及libHelloSlam.h,其中CPP文件如下:
#include<iostream>
using namespace std;
void printHello()
{
cout<<"hello SLAM"<<endl;
}
添加 CMakeLists.txt
文件內容
cmake_minimum_required( VERSION 2.8)
project( HelloSLAM)
add_executable(HelloSLAM hello.cpp)
add_library(libHello SHARED libHelloSlam.cpp)
然後,按照上述同樣的方法進行cmake以及make指令,會生成liblibHello.so
文件(共享庫,相對於靜態庫更省空間)。然後編寫庫文件的頭文件,方便其他地方調用,其頭文件如下:
#ifndef LIBHELLOSLAM_H_
#define LIBHELLOSLAM_H_
void printHello();
#endif
創建主函數,以及修改調用
#include "libHelloSlam.h"
int main( int argc, char** argv)
{
printHello();
return 0;
}
添加CMakeLists.txt
如下
cmake_minimum_required( VERSION 2.8)
project( HelloSLAM)
add_executable(HelloSLAM hello.cpp)
add_executable(useHello useHello.cpp)
target_link_libraries(useHello libHello)
add_library(libHello SHARED libHelloSlam.cpp)
至此,完成最後的cmake,以及make指令就可以實現調用庫函數輸出了。
在實際使用C++工程中,常採用這種方法實現整個工程的編譯。
一個不是很恰當的例子:Eigen庫的使用
之所以選用eigen庫,是因爲eigen庫在APM,PX4以及SLAM中都會用到,是一個常見的高效的矩陣運算庫。在Ubuntu的安裝方法爲:
sudo apt-get install libeigen3-dev
默認情況下,該庫會安裝在/usr/include/eigen3
下。之所以說是一個不是很恰當的例子,是由於該庫只有頭文件,沒有對應的cpp文件。其對應的CMakeLists.txt
文件爲
cmake_minimum_required( VERSION 2.8)
project( HelloSLAM)
add_executable(HelloSLAM hello.cpp)
add_executable(useHello useHello.cpp)
target_link_libraries(useHello libHello)
add_library(libHello SHARED libHelloSlam.cpp)
include_directories("/usr/include/eigen3")
add_executable(eigen_learn eigen_learn.cpp)
給出參考的eigen_learn.cpp
文件如下:
#include <iostream>
#include <cmath>
using namespace std;
#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Geometry>
using namespace Eigen;
int main(int argc, char** argv)
{
Matrix3d rotation_matrix = Eigen::Matrix3d::Identity();
cout<<rotation_matrix<<endl; //output the 3*3 identity matrix
rotation_matrix<<1,2,3,4,5,6,7,8,9; //init the value
Eigen::AngleAxisd rotation_vection (double(0.25*M_PI), Eigen::Vector3d::UnitZ());
//rotation 45 degree along z axis
cout<<"rotation_vection=\n"<<rotation_vection.matrix()<<endl;
cout<<"rotation_matrix(0,0)="<<rotation_matrix(0,0)<<endl;
cout<<"rotation_matrix(2,2)="<<rotation_matrix(2,2)<<endl;
rotation_matrix = rotation_vection.toRotationMatrix();
cout<<rotation_matrix<<endl;
Eigen::Vector3d v(1,0,0);
Eigen::Vector3d v_rotation = rotation_matrix*v;
cout<<v_rotation.transpose()<<endl;
cout<<v_rotation<<endl;
//quat
Eigen::Quaterniond q;
q = Eigen::Quaterniond(rotation_vection);
cout<<q.coeffs()<<endl;
q = Eigen::Quaterniond(rotation_matrix);
cout<<q.coeffs()<<endl;
return 0;
}
注:先包含頭文件,才能使用using namespace Eigen;
使用命名空間後,Eigen::
可以省去。上述程序的輸出結果爲: