VSLAM依赖库版本配置(CMake)
在开发VSLAM程序的过程中,我需要不断地学习开源程序,如ORB-SLAM2、SVO、LSD-SLAM、DSO、LearnVIORB、ygz-stereo-inertial、svo_degelet、ORB-TGZ-SLAM等等,其中依赖的库较多,主要的有OpenCV、g2o、eigen3、Sophus等。
不同的程序使用的版本往往不一样,由于需要学习不同的开源程序并转化为自己的知识,进而开发自己的 程序,因此,无法在ubuntu系统中安装一个版本适应所有开源程序。因此,需要配置选择安装于系统之外的依赖库,本博客暂时介绍OpenCV、g2o、eigen3的配置选择方法。
1 OpenCV
1.1 安装OpenCV时
注意使用-D CMAKE_INSTALL_PREFIX设置安装路径。
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/somepath/local ..
to build with modules from opencv_contrib set OPENCV_EXTRA_MODULES_PATH to <path to opencv_contrib/modules/>
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/somepath/local -D OPENCV_EXTRA_MODULES_PATH=/somepath/opencv_contrib/modules/ ..
示例:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/huicanlin/Downloads/software/opencv-3.2.0/local -D OPENCV_EXTRA_MODULES_PATH=/home/huicanlin/Downloads/software/opencv-3.2.0/opencv_contrib-3.2.0/modules/ ..
1.2 配置VSLAM程序CMakeLists.txt
在FIND_PACKAGE()之前,设置OpenCV_DIR,如下所示:
set(OpenCV_DIR "/home/huicanlin/Downloads/software/opencv-2.4.13.5/local/share/OpenCV/")#huicanlin FIND_PACKAGE(OpenCV REQUIRED)
2 g2o
2.1安装g2o时
cd workspace git clone https://github.com/RainerKuemmerle/g2o.git cd g2o mkdir build cd build cmake .. make sudo make install
If you don't want to make a system install, then you can replace the cmake command with
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=$HOME/installdir
例如在2.2时,我是这么做的:
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=../local
2.2使用g2o时
在FIND_PACKAGE()之前,设置G2O_ROOT,如下所示:
set(G2O_ROOT "/home/huicanlin/src/svo_edgelet/Thirdparty/g2o/local/")#$HOME/installdir FIND_PACKAGE(G2O REQUIRED)
可能需要如下操作,才能生效:
cd .. rm -r build mkdir build cd build cmake ..
2.3 碰到问题
在调试高翔的slambook的ch10的g2o_custombundle的时候,原CMakeLists.txt关于g2o的配置:
set(G2O_ROOT "/home/huicanlin/src/slambook/3rdparty/g2o/local/") # huicanlin added.
Find_Package(G2O REQUIRED)
include_directories( ${G2O_INCLUDE_DIR} ) # huicanlin added. SET(G2O_LIBS ... g2o_core g2o_interface ...)
target_link_libraries(... ${G2O_LIBS} ...)
make的时候,有个.h文件找不到.
因此在Find_Package()下面,加了如上所示的一行;为了与高翔提供的g2o版本一致,又在Find_Package()上面,加了如上所示的1行.make能够通过。
但是,运行的时候出现错误:Segmentation fault,在gdb中调试,发现问题来自:/opt/ros/indigo/lib/libg2o_core.so.
我分析错误在于“SET(G2O_LIBS ... g2o_core g2o_interface ...)”的时候,其实指向了ROS下的.so文件,也就是说,include的是Find_Package()找到的文件,但是库文件却是ROS下的。若是版本不同,容易出现错误。
修改方法:将g2o_core改为${G2O_CORE_LIBRARY}
SET(G2O_LIBS ... ${G2O_CORE_LIBRARY} g2o_interface ...)
然后重新编译:
rm -r /build
cd build
cmake ..
make -j4
再次运行就没问题了。
3 eigen3
3.1 安装时
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=../local
3.2 使用时
#set(EIGEN3_INCLUDE_DIR "/home/huicanlin/Downloads/software/eigen-3.2.10/local/include/eigen3/")
find_package(Eigen3 REQUIRED)
以下4-7为编译S-PTAM时需要的依赖:
4 DBow2
4.1安装
git clone git@github.com:dorian3d/DBoW2.git
Tested until commit 82401cad2cfe7aa28ee6f6afb01ce3ffa0f59b44
git checkout 82401cad2cfe7aa28ee6f6afb01ce3ffa0f59b44
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=../local
make -j4
make install
4.2 使用
set(DBoW2_DIR "../../Thirdparty/DBoW2/build") #DBoW2Config.cmake,其它路径,有此文件即可 find_package(DBoW2 REQUIRED)
5 Dlib
5.1 安装
git clone git@github.com:dorian3d/DLib.git Tested until commit 70089a38056e8aebd5a2ebacbcb67d3751433f32 git checkout 70089a38056e8aebd5a2ebacbcb67d3751433f32 mkidr build cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=../local
make -j4
make install
5.2 使用
set(DLib_DIR "../../Thirdparty/DLib/build") #DLibConfig.cmake,其它路径,有此文件即可
find_package(DLib REQUIRED)
6 DLoopDetector
6.1安装
git clone git@github.com:dorian3d/DLoopDetector.git
Tested until commit 8e62f8ae84d583d9ab67796f779272b0850571ce
git checkout 8e62f8ae84d583d9ab67796f779272b0850571ce
mkidr build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=../local
make -j4
make install
6.2 使用
set(DLoopDetector_DIR "../../Thirdparty/DLoopDetector/build") #huicanlin find_package(DLoopDetector REQUIRED)
7 opengv
7.1 安装
git clone git@github.com:laurentkneip/opengv.git Tested until commit 2e2d21917fd2fb75f2134e6d5be7a2536cbc7eb1 git checkout 2e2d21917fd2fb75f2134e6d5be7a2536cbc7eb1
mkidr build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=../local
make -j4
make install
7.2 使用
set(OPENGV_ROOT "/home/huicanlin/catkin_ws/src/sptam/Thirdparty/g2o/local/")#$HOME/installdir find_package(OpenGV REQUIRED)
8 gtsam
8.1 安装
源码使用高翔的slambook/3rdparty/gtsam
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/home/huicanlin/src/slambook/3rdparty/gtsam/local/
make -j4
make install
注意:安装路径必须时绝对路径,否则会报错!
8.2 使用
使用示例,高翔的slambook/ch11:
注意:前两行是我在cmake ..的时候,根据Terminal报错的提示增加的,如此才能够编译通过。
set(GTSAM_DIR "/home/huicanlin/src/slambook/3rdparty/gtsam/local/lib/cmake/GTSAM/") # huicanlin set(GTSAMCMakeTools_DIR "/home/huicanlin/src/slambook/3rdparty/gtsam/local/lib/cmake/GTSAMCMakeTools/") # huicanlin find_package( GTSAMCMakeTools ) find_package( GTSAM REQUIRED ) include_directories( ${GTSAM_INCLUDE_DIR} )
Find_Package(G2O REQUIRED)