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::
可以省去。上述程序的输出结果为: