ros_catkin_make - yuhannah/skills_map GitHub Wiki

ROS 工程编译说明(ubuntu16.04)

(一)准备工作

工程目录

|-sources
|---graph_slam
|-----build
|-----devel
|-----install
|-----Thirdparty
|--------package1
|--------package2
|--------package3
|--------...
|--------CMakeLists.txt
|-----CMakeLists.txt
|-----README.md

安装依赖库

尽量采用系统安装的库,以及安装ROS kinetic时自带的库。代码根据已有库的版本进行回退。不建议安装新版本库,避免编译过程复杂不可控。

安装系统库

eigen自行安装多版本管理[[参考:(八)eigen安装和多版本管理]](##(八)eigen安装和多版本管理):3.2.92/3.3.4/3.3.9/3.4.0

cmake自行安装多版本管理[[参考:(七)cmake安装和多版本管理]](##(七)cmake安装和多版本管理):3.5.1/3.14.0

ROS自行安装16.04对应的kinetic版本。

karto需要的依赖:

sudo apt-get install libeigen3-dev

gmapping需要的其他依赖:无

ceres-solver需要的其他依赖:

sudo apt-get install libsuitesparse-dev libgflags-dev libgoogle-glog-dev

cartographer需要的其他依赖:

sudo apt-get install liblua5.2-dev libprotobuf-dev protobuf-compiler

absl需要的其他依赖:

sudo apt-get install ninja-build stow

安装absl库

编译cartographer时,提示找不到absl,需要安装。

cartographer/scripts/里面有安装absl的脚本,可以直接调用安装。(该脚本在commit:ade7605版本才添加)

查看install_abseil.sh的内容,如下:

#!/bin/sh

set -o errexit
set -o verbose
git clone https://github.com/abseil/abseil-cpp.git
cd abseil-cpp
git checkout 215105818dfde3174fe799600bb0f3cae233d0bf # 20211102.0
mkdir build
cd build
cmake -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-DCMAKE_INSTALL_PREFIX=/usr/local/stow/absl \
..
ninja
sudo ninja install
cd /usr/local/stow
sudo stow absl

流程为:下载源码,切换版本,用ninja编译及安装。需要提前安装好ninja和stow[参考:安装系统库]

安装完成后直接调用脚本完成安装:

cd cartographer/scripts/
./install_abseil.sh

遇到的问题:

  • 问题1:

    如果没有提前安装好ninja和stow,或者出现编译问题,待安装ninja和stow后或者解决编译问题后,不能重复使用脚本,因为部分命令已经执行。

    解决方案:清空编译内容cartographer/scripts/abseil-cpp/build/,手动复制剩下的命令继续编译安装。

  • 问题2:

    提示c++11编译不支持,给abseil-cpp/CMakeLists.txt添加c++11支持:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

    清空编译内容,重新编译通过。

(二)ROS 编译遇到的问题整理

  1. ROS默认的工作空间目录为workspace/src,如果工程不是默认目录结构,无法直接调用catkin_make和catkin_make_isolated命令

    编译提示:

    $ catkin_make Base path: /home/yu/sources/graph_slam The specified source space "/home/yu/sources/graph_slam/src" does not exist

    $ catkin_make_isolated Could not find source space: /home/yu/sources/graph_slam/src

    解决方案:在catkin_make或者catkin_make_isolated命令后添加参数,配置源码目录--source source_path

    这是很多网络编译资料没有提及的。参考:ROS编译:catkin简析(https://www.cnblogs.com/wlzy/p/8214509.html)

  2. ROS的编译命令catkin_make无法编译包含metapackage或者non-catkin package的工程

    编译提示(包含metapackage):

    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- ~~  traversing 4 packages in topological order:
    -- ~~  - openslam_gmapping
    -- ~~  - slam_gmapping (metapackage)
    WARNING: The metapackage 'slam_gmapping' has no CMakeLists.txt. Please add one to the package source. You can use the following file: /home/yu/sources/graph_slam/build_test/Thirdparty/catkin_generated/metapackages/slam_gmapping/CMakeLists.txt
    -- ~~  - convert_clf_to_bag
    -- ~~  - gmapping
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- +++ processing catkin metapackage: 'slam_gmapping'
    -- ==> add_subdirectory(slam_gmapping/slam_gmapping) (using generated file from <buildspace>/catkin_generated/metapackages/slam_gmapping)
    WARNING: Add a CMakeLists.txt file to the metapackage 'slam_gmapping'
    CMake Error: File /home/yu/sources/graph_slam/build_test/Thirdparty/catkin_generated/metapackages/slam_gmapping/package.xml does not exist.
    CMake Error at /opt/ros/kinetic/share/catkin/cmake/stamp.cmake:10 (configure_file):
    configure_file Problem configuring file
    Call Stack (most recent call first):
    /opt/ros/kinetic/share/catkin/cmake/catkin_package_xml.cmake:72 (stamp)
    /opt/ros/kinetic/share/catkin/cmake/catkin_package_xml.cmake:50 (_catkin_package_xml)
    /opt/ros/kinetic/share/catkin/cmake/catkin_metapackage.cmake:39 (catkin_package_xml)
    build_test/Thirdparty/catkin_generated/metapackages/slam_gmapping/CMakeLists.txt:4 (catkin_metapackage)

    编译提示(包含non-catkin package):

    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    -- ~~  traversing 6 packages in topological order:
    -- ~~  - ceres-solver (plain cmake)
    -- ~~  - cartographer (plain cmake)
    -- ~~  - cartographer_ros_msgs
    -- ~~  - convert_clf_to_bag
    -- ~~  - cartographer_ros
    -- ~~  - cartographer_rviz
    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkin_workspace.cmake:100 (message):
    This workspace contains non-catkin packages in it, and catkin cannot build
    a non-homogeneous workspace without isolation.  Try the
    'catkin_make_isolated' command instead.
    Call Stack (most recent call first):
    Thirdparty/CMakeLists.txt:69 (catkin_workspace)

    此时只能用catkin_make_isolated命令,独立编译每个包。

    # catkin_make_isolated编译命令
    cd graph_slam/
    catkin_make_isolated \
     --source . \
     --build ./build_isolated \
     --devel ./devel_isolated \
     --install-space ./install_isolated
  3. 每次切换分支后,最好删除已有的编译目录build_*devel_*,避免遗留的安装库对下次编译过程产生影响

  4. ROS的编译命令catkin_make是对cmake的进一步封装,因此是可以用cmake命令等效编译的

    # catkin_make编译命令
    cd graph_slam/
    catkin_make \
     --source . \
     --build ./build_pc \
     -DCATKIN_DEVEL_PREFIX=./devel_pc \
     -DCMAKE_INSTALL_PREFIX=./install
    
    # cmake编译命令
    cd graph_slam/
    mkdir build_pc
    cd build_pc
    cmake .. \
     -DCMAKE_INSTALL_PREFIX=../install  \
     -DCATKIN_DEVEL_PREFIX=../devel_pc

    即catkin_make能实现的编译,用cmake也能实现。但是catkin_make不能实现的编译,简单的cmake一般也无法实现。

  5. 使用catkin_make配置源码路径--source source_path后,配置环境变量./devel/setup.bash失效

    原因:由于source命令和linux下配置环境变量的命令一样,使用后清空了当前环境变量,导致所有bash文件失效。

    解决方案:catkin_make后再次source ~/.bashrc,再./devel/setup.bash

(三)Karto(TODO)

Karto-spa

Karto-g2o

Karto-ceres

(四)Gmapping编译和运行

编译

graph_slam/Thirdparty/中下载好源码:

cd graph_slam/Thirdparty/
git clone https://github.com/ros-perception/slam_gmapping.git
cd slam_gmapping/
git remote rm origin
rm -rf .git

cd graph_slam/Thirdparty/
git clone https://github.com/ros-perception/openslam_gmapping.git
cd openslam_gmapping/
git remote rm origin
rm -rf .git

graph_slam/build_pc/下使用catkin编译,产生错误:

# catkin_make编译命令
cd graph_slam/
catkin_make \
 --source . \
 --build ./build_pc \
 -DCATKIN_DEVEL_PREFIX=./devel_pc \
 -DCMAKE_INSTALL_PREFIX=./install

# cmake编译命令
cd graph_slam/
mkdir build_pc
cd build_pc
cmake .. \
 -DCMAKE_INSTALL_PREFIX=../install  \
 -DCATKIN_DEVEL_PREFIX=../devel_pc

报错内容:

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 4 packages in topological order:
-- ~~  - openslam_gmapping
-- ~~  - slam_gmapping (metapackage)
WARNING: The metapackage 'slam_gmapping' has no CMakeLists.txt. Please add one to the package source. You can use the following file: /home/yu/sources/graph_slam/build_test/Thirdparty/catkin_generated/metapackages/slam_gmapping/CMakeLists.txt
-- ~~  - convert_clf_to_bag
-- ~~  - gmapping
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin metapackage: 'slam_gmapping'
-- ==> add_subdirectory(slam_gmapping/slam_gmapping) (using generated file from <buildspace>/catkin_generated/metapackages/slam_gmapping)
WARNING: Add a CMakeLists.txt file to the metapackage 'slam_gmapping'
CMake Error: File /home/yu/sources/graph_slam/build_test/Thirdparty/catkin_generated/metapackages/slam_gmapping/package.xml does not exist.
CMake Error at /opt/ros/kinetic/share/catkin/cmake/stamp.cmake:10 (configure_file):
configure_file Problem configuring file
Call Stack (most recent call first):
/opt/ros/kinetic/share/catkin/cmake/catkin_package_xml.cmake:72 (stamp)
/opt/ros/kinetic/share/catkin/cmake/catkin_package_xml.cmake:50 (_catkin_package_xml)
/opt/ros/kinetic/share/catkin/cmake/catkin_metapackage.cmake:39 (catkin_package_xml)
build_test/Thirdparty/catkin_generated/metapackages/slam_gmapping/CMakeLists.txt:4 (catkin_metapackage)

提示项目中包含metapackage,建议用catkin_make_isolated命令。

# catkin_make_isolated编译命令
cd graph_slam/
catkin_make_isolated \
 --source . \
 --build ./build_isolated \
 --devel ./devel_isolated \
 --install-space ./install_isolated

其中,--build ./build_isolated--devel ./devel_isolated--install-space ./install_isolated都是默认参数,不修改时可以省略。

从编译结果可以看出catkin_make_isolated的编译过程:

Base path: /home/yu/sources/graph_slam
Source space: /home/yu/sources/graph_slam
Build space: /home/yu/sources/graph_slam/build_isolated
Devel space: /home/yu/sources/graph_slam/devel_isolated
Install space: /home/yu/sources/graph_slam/install_isolated
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~  traversing 4 packages in topological order:
~~  - convert_clf_to_bag
~~  - openslam_gmapping
~~  - gmapping
~~  - slam_gmapping
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The packages or cmake arguments have changed, forcing cmake invocation

==> Processing catkin package: 'openslam_gmapping'
==> Creating build directory: 'build_isolated/openslam_gmapping'
==> Building with env: '/home/yu/sources/graph_slam/devel_isolated/convert_clf_to_bag/env.sh'
==> cmake /home/yu/sources/graph_slam/Thirdparty/openslam_gmapping -DCATKIN_DEVEL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/openslam_gmapping -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/install_isolated -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/openslam_gmapping'
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/openslam_gmapping'
<== Finished processing package [2 of 4]: 'openslam_gmapping'

==> Processing catkin package: 'gmapping'
==> Creating build directory: 'build_isolated/gmapping'
==> Building with env: '/home/yu/sources/graph_slam/devel_isolated/openslam_gmapping/env.sh'
==> cmake /home/yu/sources/graph_slam/Thirdparty/slam_gmapping/gmapping -DCATKIN_DEVEL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/gmapping -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/install_isolated -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/gmapping'
-- Using CMAKE_PREFIX_PATH: /home/yu/sources/graph_slam/devel_isolated/openslam_gmapping;/home/yu/sources/graph_slam/devel_isolated/convert_clf_to_bag;/opt/ros/kinetic
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/gmapping'
<== Finished processing package [3 of 4]: 'gmapping'

==> Processing catkin package: 'slam_gmapping'
==> Creating build directory: 'build_isolated/slam_gmapping'
==> Building with env: '/home/yu/sources/graph_slam/devel_isolated/gmapping/env.sh'
==> cmake /home/yu/sources/graph_slam/Thirdparty/slam_gmapping/slam_gmapping -DCATKIN_DEVEL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/slam_gmapping -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/install_isolated -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/slam_gmapping'
-- Using CMAKE_PREFIX_PATH: /home/yu/sources/graph_slam/devel_isolated/gmapping;/home/yu/sources/graph_slam/devel_isolated/openslam_gmapping;/home/yu/sources/graph_slam/devel_isolated/convert_clf_to_bag;/opt/ros/kinetic
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/slam_gmapping'
<== Finished processing package [4 of 4]: 'slam_gmapping'

运行

cd graph_slam
source ~/.bashrc
source ./devel_isolated/setup.bash
roslaunch gmapping xx.launch

其他编译过程的记录

此处记录编译过程的其他内容,遇到的问题和解决方案。

在会用catkin_make_isolated命令前,尝试用cmake命令依次编译openslam_gmapping和slam_gmapping,后来发现也就是上述编译过程的cmake命令的提取,重点在于CMAKE_PREFIX_PATH的配置,过程如下:

$ cd graph_slam
$ mkdir build_pc
$ cd build_pc
$ cmake \
  ../Thirdparty/openslam_gmapping/CMakeLists.txt \       # 先编译openslam_gmapping
  -DCMAKE_INSTALL_PREFIX=../install \
  -DCATKIN_DEVEL_PREFIX=../devel_pc/openslam_gmapping \  # 指定编译结果目录
  -B .     # 指定编译过程目录
$ make -j2 # 编译生成openslam_gmapping库

$ rm -rf * # 删除编译过程文件
$ cmake \
  ../Thirdparty/slam_gmapping/gmapping/CMakeLists.txt \  # 再编译slam_gmapping/gmapping
  -DCMAKE_INSTALL_PREFIX=../install \
  -DCATKIN_DEVEL_PREFIX=../devel_pc/slam_gmapping/gmapping \
  -DCMAKE_PREFIX_PATH="/home/yu/sources/graph_slam/devel_pc/openslam_gmapping;/opt/ros/kinetic" \ # 指定生成的openslam_gmapping库目录和ROS库目录
  -B .
$ make -j2 # 编译生成slam_gmapping库

$ rm -rf *
$ cmake \
  ../Thirdparty/slam_gmapping/slam_gmapping/CMakeLists.txt \
  -DCMAKE_INSTALL_PREFIX=../install \
  -DCATKIN_DEVEL_PREFIX=../devel_pc/slam_gmapping/slam_gmapping \
  -DCMAKE_PREFIX_PATH="/home/yu/sources/graph_slam/devel_pc/openslam_gmapping;/opt/ros/kinetic;/home/yu/sources/graph_slam/devel_pc/slam_gmapping/gmapping" \
  -B .
$ make -j2

运行时配置gmapping库所在的setup.bash:

$ cd graph_slam/
$ source ~/.bashrc
$ source ./devel_pc/slam_gmapping/slam_gmapping/setup.bash
$ roslaunch gmapping slam_gmapping_test_pc.launch

(五)Cartographer编译和运行

编译

此处提供的是ceres-solver+cartographer+cartographer_ros三个库的完整编译说明:

  • 不需要提前单独编译安装ceres-solver和cartographer
  • ceres-solver和cartographer都编译并安装在了项目的默认路径devel_isolated
  • 如果已经安装了ceres-solver或者cartographer的某个版本到本地,不会对编译造成影响(不确定对运行是否有影响?)
  • 单独编译安装ceres-solver可以参考[(六)ceres-solver编译安装或编译导出]
  • 单独编译安装cartographer可以参考2. 编译cartographer(####2. 编译cartographer)

graph_slam/Thirdparty/中下载好源码,并切换到指定版本:

cd graph_slam/Thirdparty/
git clone https://github.com/ceres-solver/ceres-solver.git
cd ceres-solver/
git checkout 33dd469a53383743af00a711a6a85e64c35177e8
git remote rm origin
rm -rf .git

cd graph_slam/Thirdparty/
git clone https://github.com/cartographer-project/cartographer.git
cd cartographer/
git checkout 028391357d4d5fc680bd2eeadf6ff9723973ff57
git remote rm origin
rm -rf .git

cd graph_slam/Thirdparty
git clone https://github.com/cartographer-project/cartographer_ros.git
cd cartographer_ros/
git checkout b56903c1bb965fae2309312d4af757a6658c2ab0
git remote rm origin
rm -rf .git

graph_slam/build_pc/下使用catkin编译,产生错误:

# catkin_make编译命令
cd graph_slam/
catkin_make \
 --source . \
 --build ./build_pc \
 -DCATKIN_DEVEL_PREFIX=./devel_pc \
 -DCMAKE_INSTALL_PREFIX=./install

# cmake编译命令
cd graph_slam/
mkdir build_pc
cd build_pc
cmake .. \
 -DCMAKE_INSTALL_PREFIX=../install  \
 -DCATKIN_DEVEL_PREFIX=../devel_pc

报错内容:

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 6 packages in topological order:
-- ~~  - ceres-solver (plain cmake)
-- ~~  - cartographer (plain cmake)
-- ~~  - cartographer_ros_msgs
-- ~~  - convert_clf_to_bag
-- ~~  - cartographer_ros
-- ~~  - cartographer_rviz
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CMake Error at /opt/ros/kinetic/share/catkin/cmake/catkin_workspace.cmake:100 (message):
This workspace contains non-catkin packages in it, and catkin cannot build
a non-homogeneous workspace without isolation.  Try the
'catkin_make_isolated' command instead.
Call Stack (most recent call first):
Thirdparty/CMakeLists.txt:69 (catkin_workspace)

提示项目中包含non-catkin包(纯cmake包),建议用catkin_make_isolated命令。

# catkin_make_isolated编译命令
cd graph_slam/
catkin_make_isolated \
 --source . \
 --build ./build_isolated \
 --devel ./devel_isolated \
 --install-space ./install_isolated

其中,--build ./build_isolated--devel ./devel_isolated--install-space ./install_isolated都是默认参数,不修改时可以省略。

从编译结果可以看出catkin_make_isolated的编译过程:

# 1.source路径+build路径+devel路径+install路径
Base path: /home/yu/sources/graph_slam
Source space: /home/yu/sources/graph_slam
Build space: /home/yu/sources/graph_slam/build_isolated
Devel space: /home/yu/sources/graph_slam/devel_isolated
Install space: /home/yu/sources/graph_slam/install_isolated

# 2.一共编译几个package,按什么顺序
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~  traversing 6 packages in topological order:
~~  - cartographer_ros_msgs
~~  - ceres-solver (plain cmake)
~~  - cartographer (plain cmake)
~~  - cartographer_ros
~~  - cartographer_rviz
~~  - convert_clf_to_bag
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The packages or cmake arguments have changed, forcing cmake invocation

# 3.编译cartographer_ros_msgs
==> Processing catkin package: 'cartographer_ros_msgs'
# 3.1.创建编译子目录
==> Creating build directory: 'build_isolated/cartographer_ros_msgs'
# 3.2.执行的cmake命令:指定devel路径+install路径
==> cmake /home/yu/sources/graph_slam/Thirdparty/cartographer_ros/cartographer_ros_msgs -DCATKIN_DEVEL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/cartographer_ros_msgs -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/install_isolated -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/cartographer_ros_msgs'
# 3.3.cmake完成
-- Build files have been written to: /home/yu/sources/graph_slam/build_isolated/cartographer_ros_msgs
# 3.4.执行make
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/cartographer_ros_msgs'
<== Finished processing package [1 of 6]: 'cartographer_ros_msgs'

# 4.编译ceres-solver
==> Processing plain cmake package: 'ceres-solver'
==> Creating build directory: 'build_isolated/ceres-solver/devel'
==> Building with env: '/home/yu/sources/graph_slam/devel_isolated/cartographer_ros_msgs/env.sh'
# 4.1.执行的cmake命令:指定install路径
==> cmake /home/yu/sources/graph_slam/Thirdparty/ceres-solver -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/ceres-solver -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/ceres-solver/devel'
# 4.2.cmake完成
-- Build files have been written to: /home/yu/sources/graph_slam/build_isolated/ceres-solver/devel
# 4.3.执行make
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/ceres-solver/devel'
# 4.4.执行make install
==> make install in '/home/yu/sources/graph_slam/build_isolated/ceres-solver/devel'
-- Installing: /home/yu/sources/graph_slam/devel_isolated/ceres-solver/lib/libceres.a
==> Generating an env.sh
<== Finished processing package [2 of 6]: 'ceres-solver'

# 5.编译cartographer
==> Processing plain cmake package: 'cartographer'
==> Creating build directory: 'build_isolated/cartographer/devel'
# 5.1.是否根据这个文件找到ceres-solver库的?
==> Building with env: '/home/yu/sources/graph_slam/devel_isolated/ceres-solver/env.sh'
==> cmake /home/yu/sources/graph_slam/Thirdparty/cartographer -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/cartographer -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/cartographer/devel'
# 5.2.找到ceres-solver:在/devel_isolated/ceres-solver/
-- Found Ceres version: 2.0.0 installed in: /home/yu/sources/graph_slam/devel_isolated/ceres-solver with components: [EigenSparse, SparseLinearAlgebraLibrary, LAPACK, SuiteSparse, CXSparse, SchurSpecializations]
-- Build files have been written to: /home/yu/sources/graph_slam/build_isolated/cartographer/devel
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/cartographer/devel'
==> make install in '/home/yu/sources/graph_slam/build_isolated/cartographer/devel'
-- Installing: /home/yu/sources/graph_slam/devel_isolated/cartographer/share/cartographer/cartographer-config.cmake
==> Generating an env.sh
<== Finished processing package [3 of 6]: 'cartographer'

# 6.编译cartographer_ros
==> Processing catkin package: 'cartographer_ros'
==> Creating build directory: 'build_isolated/cartographer_ros'
==> Building with env: '/home/yu/sources/graph_slam/devel_isolated/cartographer/env.sh'
==> cmake /home/yu/sources/graph_slam/Thirdparty/cartographer_ros/cartographer_ros -DCATKIN_DEVEL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/cartographer_ros -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/install_isolated -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/cartographer_ros'
# 6.1.这里使用了CMAKE_PREFIX_PATH找到了ceres-solver+cartographer+cartographer_ros_msgs
-- Using CMAKE_PREFIX_PATH: /home/yu/sources/graph_slam/devel_isolated/cartographer;/home/yu/sources/graph_slam/devel_isolated/ceres-solver;/home/yu/sources/graph_slam/devel_isolated/cartographer_ros_msgs;/opt/ros/kinetic
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/cartographer_ros'
<== Finished processing package [4 of 6]: 'cartographer_ros'

# 7.编译cartographer_rviz
==> Processing catkin package: 'cartographer_rviz'
==> Creating build directory: 'build_isolated/cartographer_rviz'
==> Building with env: '/home/yu/sources/graph_slam/devel_isolated/cartographer_ros/env.sh'
==> cmake /home/yu/sources/graph_slam/Thirdparty/cartographer_ros/cartographer_rviz -DCATKIN_DEVEL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/cartographer_rviz -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/install_isolated -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/cartographer_rviz'
-- Using CMAKE_PREFIX_PATH: /home/yu/sources/graph_slam/devel_isolated/cartographer_ros;/home/yu/sources/graph_slam/devel_isolated/cartographer;/home/yu/sources/graph_slam/devel_isolated/ceres-solver;/home/yu/sources/graph_slam/devel_isolated/cartographer_ros_msgs;/opt/ros/kinetic
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/cartographer_rviz'
<== Finished processing package [5 of 6]: 'cartographer_rviz'

# 8.编译convert_clf_to_bag
==> Processing catkin package: 'convert_clf_to_bag'
==> Creating build directory: 'build_isolated/convert_clf_to_bag'
==> Building with env: '/home/yu/sources/graph_slam/devel_isolated/cartographer_rviz/env.sh'
==> cmake /home/yu/sources/graph_slam/Thirdparty/convert_clf_to_bag -DCATKIN_DEVEL_PREFIX=/home/yu/sources/graph_slam/devel_isolated/convert_clf_to_bag -DCMAKE_INSTALL_PREFIX=/home/yu/sources/graph_slam/install_isolated -G Unix Makefiles in '/home/yu/sources/graph_slam/build_isolated/convert_clf_to_bag'
-- Using CMAKE_PREFIX_PATH: /home/yu/sources/graph_slam/devel_isolated/cartographer_rviz;/home/yu/sources/graph_slam/devel_isolated/cartographer_ros;/home/yu/sources/graph_slam/devel_isolated/cartographer;/home/yu/sources/graph_slam/devel_isolated/ceres-solver;/home/yu/sources/graph_slam/devel_isolated/cartographer_ros_msgs;/opt/ros/kinetic
==> make -j8 -l8 in '/home/yu/sources/graph_slam/build_isolated/convert_clf_to_bag'
<== Finished processing package [6 of 6]: 'convert_clf_to_bag'

运行

cd graph_slam
source ~/.bashrc
source ./devel_isolated/setup.bash
roslaunch cartographer_ros xx.launch bag_filename:=xx.bag

其他编译过程的记录

此处记录编译过程的其他内容,遇到的问题和解决方案。

1. 编译ceres-solver

编译过程参考[(六)ceres-solver编译安装或编译导出],在graph_slam/build_ceres/目录下编译并导出编译结果。

cd graph_slam/
mkdir build_ceres
cd build_ceres/
cmake \
 ../Thirdparty/ceres-solver/CMakeLists.txt
 -DEXPORT_BUILD_DIR=ON \
 -DBUILD_SHARED_LIBS=ON \
 -B .
make -j2

2. 编译cartographer

当前版本信息2.0.0:

commit: ef00de2317dcf7895b09f18cc4d87f8b533a019b

Author: Xùdōng Yáng [email protected] 2022-09-07 03:44:43

Committer: GitHub [email protected] 2022-09-07 03:44:43

Branches: master, remotes/origin/master

Follows: 2.0.0

查看CMakeLists.txt,整理依赖库需求:

find_package(absl REQUIRED)
find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})
find_package(Ceres REQUIRED COMPONENTS SuiteSparse)
find_package(Eigen3 REQUIRED)
find_package(LuaGoogle REQUIRED)
find_package(Protobuf 3.0.0 REQUIRED)
find_package(Sphinx)

对照文末的[(九)系统安装和源码安装的库]的版本信息,或者执行cmake,查看结果:

(1)提示absl找不到

CMake Error at CMakeLists.txt:32 (find_package):

By not providing "Findabsl.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "absl", but CMake did not find one.

Could not find a package configuration file provided by "absl" with any of the following names:

​ abslConfig.cmake

​ absl-config.cmake

Add the installation prefix of "absl" to CMAKE_PREFIX_PATH or set "absl_DIR" to a directory containing one of the above files.

If "absl" provides a separate development package or SDK, be sure it has been installed.

(2)提示protobuf版本不满足需求:当前2.6.1->需要3.3.0

CMake Error at /opt/cmake-3.14.0/share/cmake-3.14/Modules/FindPackageHandleStandardArgs.cmake:137 (message):

Could NOT find Protobuf: Found unsuitable version "2.6.1", but required is vat least "3.0.0" (found /usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread)

Call Stack (most recent call first):

​ /opt/cmake-3.14.0/share/cmake-3.14/Modules/FindPackageHandleStandardArgs.cmake:376 (_FPHSA_FAILURE_MESSAGE)

​ /opt/cmake-3.14.0/share/cmake-3.14/Modules/FindProtobuf.cmake:594 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)

CMakeLists.txt:47 (find_package)

使用cartographer/scripts/自带的脚本安装absl,参考[安装absl库]

将cartographer代码版本回退到符合当前库版本的commit:

commit: 028391357d4d5fc680bd2eeadf6ff9723973ff57

Author: Jihoon Lee [email protected] 2017-11-10 00:19:29

Committer: Wally B. Feed [email protected] 2017-11-10 00:19:29

Branches: master, remotes/origin/master

Follows: 0.2.0

编译安装cartographer0.2.0,配置安装目录(将cartographer安装在指定目录,便于查找库):

cd graph_slam/
mkdir build_pc
cd build_pc/  
cmake \
../Thirdparty/cartographer/CMakeLists.txt \
-DCMAKE_INSTALL_PREFIX=../install/cartographer \
-B .
make -j2
make install

这里感觉catkin编译的区别,catkin编译的过程文件在build,lib和bin在devel,清空编译内容不会清空devel下的lib和bin。但是cmake编译的过程文件和lib和bin都在build下,必须经过install才能将lib和bin拷贝到其他目录,供调用,清空编译内容同时也清空了lib和bin。

3. 编译cartographer_ros

cartographer_ros目录下包含三个子目录:cartographer_ros_msgs,cartographer_ros,cartographer_rviz。

查看package.xml能看出子目录的依赖关系:

# cartographer_ros/package.xml
<depend>cartographer</depend>
<depend>cartographer_ros_msgs</depend>

# cartographer_rviz/package.xml
<depend>cartographer</depend>
<depend>cartographer_ros</depend>
<depend>cartographer_ros_msgs</depend>

决定了编译顺序为:cartographer_ros_msgs-->cartographer_ros-->cartographer_rviz。

当前版本信息0.3.0:

commit: c138034db0c47fe0ea5a2abe516acae02190dbf5

Author: Bo Chen [email protected] 2022-10-28 01:36:29

Committer: GitHub [email protected] 2022-10-28 01:36:29

Branches: master, remotes/origin/master

Follows: 0.3.0

没有额外的依赖库需求。编译过程出错,与cartographer版本不匹配。

将cartographer_ros代码版本回退到符合当前cartographer库版本的commit:

commit: b56903c1bb965fae2309312d4af757a6658c2ab0

Author: Michael Grupp [email protected] 2017-11-14 21:49:19

Committer: Wally B. Feed [email protected] 2017-11-14 21:49:19

Branches: master, remotes/origin/master, remotes/origin/release-1.0, remotes/origin/ros2-dashing, remotes/origin/ros2-dashing-1.0.0

Follows: 0.2.0

编译cartographer_ros0.2.0:

3.1. 编译cartographer_ros_msgs

cd graph_slam/
mkdir build_pc
cd build_pc/  
cmake \
../Thirdparty/cartographer_ros/cartographer_ros_msgs/CMakeLists.txt \
-DCMAKE_INSTALL_PREFIX=../install/cartographer_ros_msgs \
-DCATKIN_DEVEL_PREFIX=../devel_pc/cartographer_ros_msgs \
-B .
make -j2

3.2. 编译cartographer_ros

要配置编译好的cartographer和cartographer_ros_msgs库目录到CMAKE_PREFIX_PATH

cd graph_slam/
mkdir build_pc
cd build_pc/  
cmake \
../Thirdparty/cartographer_ros/cartographer_ros/CMakeLists.txt \
-DCMAKE_INSTALL_PREFIX=../install/cartographer_ros \
-DCATKIN_DEVEL_PREFIX=../devel_pc/cartographer_ros \
-DCMAKE_PREFIX_PATH="/home/yu/sources/graph_slam/install/cartographer;/home/yu/sources/graph_slam/devel_pc/cartographer_ros_msgs;/opt/ros/kinetic"
-B .
make -j2

3.3. 编译cartographer_rviz

要配置编译好的cartographer和cartographer_ros_msgs和cartographer_ros库目录到CMAKE_PREFIX_PATH

cd graph_slam/
mkdir build_pc
cd build_pc/  
cmake \
../Thirdparty/cartographer_ros/cartographer_ros/CMakeLists.txt \
-DCMAKE_INSTALL_PREFIX=../install/cartographer_ros \
-DCATKIN_DEVEL_PREFIX=../devel_pc/cartographer_ros \
-DCMAKE_PREFIX_PATH="/home/yu/sources/graph_slam/install/cartographer;/home/yu/sources/graph_slam/devel_pc/cartographer_ros_msgs;/opt/ros/kinetic"
-B .
make -j2

4. 运行遇到问题

运行的时候遇到问题,由于cartographer_ros的三个子包分别编译在三个devel_pc/子路径下,导致没有一个setup.bash能关联所有编译结果,导致运行时找不到节点。

最后还是得用catkin_make_isolated命令编译。

(六)ceres-solver编译安装或编译导出

源码仓库: https://github.com/ceres-solver/ceres-solver.git

当前版本信息2.1.0:

commit: 8bf4a2f42c46c84db0bf5b3fe9deb152eed4aff5

Author: Dmitriy Korchemkin [email protected] 2023-01-31 22:13:19

Committer: Dmitriy Korchemkin [email protected] 2023-01-31 22:13:19

Branches: master, remotes/origin/master

Follows: 2.1.0

查看CMakeLists.txt,整理依赖库需求:

find_package(Eigen3 3.3 REQUIRED)
find_package(CUDA QUIET)
find_package(CUDAToolkit QUIET)
find_package(LAPACK QUIET)
find_package(SuiteSparse 4.5.6 COMPONENTS CHOLMOD SPQR OPTIONAL_COMPONENTS Partition)
find_package(METIS)
find_package(AccelerateSparse)
find_package(gflags 2.2.0)
find_package(Glog)
find_package(benchmark 1.3 QUIET)
find_package(Sphinx)

对照文末的[(九)系统安装和源码安装的库]的版本信息,或者执行cmake,查看结果:

cd ceres-solver/
mkdir build
cd build/
cmake ..

(1)提示suitesparse版本不满足需求:当前4.4.6->需要4.5.6

-- Failed to find some/all required components of SuiteSparse.: Found unsuitable version "4.4.6", but required is at least "4.5.6" (found /usr/include/suitesparse)

(2)提示gflags版本不满足需求:当前2.1.2->需要2.2.0

CMake Warning at CMakeLists.txt:408 (find_package):

By not providing "Findgflags.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "gflags", but CMake did not find one.

Could not find a package configuration file provided by "gflags" (requested version 2.2.0) with any of the following names:

​ gflagsConfig.cmake

​ gflags-config.cmake

Add the installation prefix of "gflags" to CMAKE_PREFIX_PATH or set "gflags_DIR" to a directory containing one of the above files. If "gflags" provides a separate development package or SDK, be sure it has been installed.

将ceres-solver代码版本回退到符合当前库版本的commit:

commit: 33dd469a53383743af00a711a6a85e64c35177e8

Author: NeroBurner [email protected] 2019-12-03 17:05:15

Committer: NeroBurner [email protected] 2019-12-12 21:56:00

Branches: master, remotes/origin/master, remotes/origin/threaded-jacobi, remotes/origin/use-find-package

Follows: 1.14.0

编译ceres-solver1.14.0,配置生成共享库配置导出编译目录(参考ceres-solver官网:https://izhengfan.gitbooks.io/ceres-solver-cn/content/an-zhuang.html#CMake 调用 Ceres):

cd ceres-solver/
mkdir build
cd build/  
cmake .. \
-DEXPORT_BUILD_DIR=ON \
-DBUILD_SHARED_LIBS=ON
make -j2

此处不建议安装到本地,导出编译目录的方式更适合多版本管理。如果需要安装到本地,可以配置安装目录-DCMAKE_INSTALL_PREFIX ="path"make install完成安装。

(七)cmake安装和多版本管理

主要流程:

1.已有的cmake版本保留

2.官网下载编译好的需要的cmake版本

3.解压后放在/opt下

4.建立软连接

5.第4步后还不行,继续添加环境变量

cd ~/Download
wget https://cmake.org/files/v3.14/cmake-3.14.0-Linux-x86_64.tar.gz
tar -xzvf cmake-3.14.0-Linux-x86_64.tar.gz
 
# 解压出来的包,将其放在 /opt 目录下,其他目录也可以,主要别以后不小心删了
sudo mv cmake-3.14.0-Linux-x86_64 /opt/cmake-3.14.0
 
# 建立软链接
sudo ln -sf /opt/cmake-3.14.0/bin/*  /usr/bin/
 
# 查看 cmake 版本
cmake --version

# 打开并编辑文件:
gedit ~/.bashrc

# 添加内容,PATH路径根据完整真实路径
export PATH=/home/yu/opt/cmake-3.14.2/bin:$PATH

# 更新bashrc
source ~/.bashrc

# 查看 cmake 版本
cmake --version

验证:

安装新版前的当前版本:

$ cmake --version 
cmake version 3.5.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

安装cmake-3.14.0后(到第4步就可以用了):

$ cmake --version 
cmake version 3.14.0

CMake suite maintained and supported by Kitware (kitware.com/cmake).

(八)eigen安装和多版本管理

1.保留系统安装的eigen版本相关内容

系统安装的版本为3.2.92

头文件目录:/usr/include/eigen3

包含下述文件夹和文件:

Eigen/ unsupported/ signature_of_eigen3_matrix_library

系统命令pkg-config查找文件配置的文件为eigen3.pc:/usr/share/pkg-config/eigen3.pc

文件内容为:

prefix=/usr
exec_prefix=${prefix}

Name: Eigen3
Description: A C++ template library for linear algebra: vectors, matrices, and related algorithms
Requires:
Version: 3.2.92
Libs:
Cflags: -I${prefix}/include/eigen3

查找命令(最后的eigen3对应eigen3.pc的去后缀名):

$ pkg-config --cflags --libs eigen3
-I/usr/include/eigen3
$ pkg-config --modversion eigen3
3.2.92

cmake的查找路径为:/usr/lib/cmake/eigen3/

包含以下文件:

Eigen3Config.cmake UseEigen3.cmake

查找命令为:

find_package(PkgConfig)
pkg_search_module(EIGEN3 REQUIRED eigen3)
if(EIGEN3_FOUND)
 INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIRS})
 message(STATUS "Eigen ${EIGEN3_VERSION} found (include: ${EIGEN3_INCLUDE_DIRS})")
endif()

2.将系统默认版本拷贝一份

系统默认3.2.92版本,存放在系统默认目录下,系统只查找eigen3,不管是什么版本。

拷贝一份当前eigen3.2.92,重命名为eigen3.2.92,便于替换默认目录的内容。

  • /usr/include/eigen3拷贝并重命名为/usr/include/eigen3.2.92

  • /usr/share/pkg-config/eigen3.pc拷贝并重命名为/usr/share/pkg-config/eigen3.2.92.pc

  • /usr/lib/cmake/eigen3拷贝并重命名为/usr/lib/cmake/eigen3.2.92

3.下载eigen3某个版本的源码,解压,编译,安装

cd eigen-3.4.0/
mkdir build
cd build
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/include/eigen3.4.0 \
-DCMAKE_PREFIX_PATH=/usr/include/eigen3.4.0 \
-DEIGEN_BUILD_PKGCONFIG=ON \
..
sudo make install

下载源码,创建build文件夹,编译。安装这一步,可以等同于将编译的include和share拷贝到目标路径下。

用命令方式,能避免路径配置存在问题。(比如文件中用到的相对路径等)。

这样在/usr/include 目录下,同时存在eigen3(即默认eigen3.2.92)和eigen3.4.0.的源码。

  • eigen3.4.0的源码路径是/usr/include/eigen3.4.0/include/eigen3

  • eigen3.4.0的pkgconfig路径是/usr/include/eigen3.4.0/share/pkgconfig

  • eigen3.4.0的cmake路径是/usr/include/eigen3.4.0/share/eigen3/cmake

接下来依次处理这三个部分。

4.将eigen3.4.0的其他配置同步拷贝到对应的目录下

(1)系统pkg查找文件配置:

  • /usr/include/eigen3.4.0/share/pkgconfig/eigen3.pc拷贝并重命:/usr/share/pkg-config/eigen3.4.0.pc

  • 修改文件属性:

    sudo chmod 777 eigen3.4.0.pc
  • 修改文件内容

    原文件内容:

    prefix=/usr/include/eigen3.4.0
    exec_prefix=${prefix}
    
    Name: Eigen3
    Description: A C++ template library for linear algebra: vectors, matrices, and related algorithms
    Requires:
    Version: 3.4.0
    Libs:
    Cflags: -I${prefix}/include/eigen3

    将prefix替换成/usr,用于指向默认eigen3路径。

    prefix=/usr
    exec_prefix=${prefix}
    
    Name: Eigen3
    Description: A C++ template library for linear algebra: vectors, matrices, and related algorithms
    Requires:
    Version: 3.4.0
    Libs:
    Cflags: -I${prefix}/include/eigen3

(2)cmake查找路径配置:

/usr/include/eigen3.4.0/share/eigen3/cmake下的文件拷贝到/usr/lib/cmake/eigen3.4.0/下:

Eigen3Config.cmake Eigen3ConfigVersion.cmake Eigen3Targets.cmake UseEigen3.cmake

5.替换默认eigen3

将eigen3.4.0的头文件/usr/include/eigen3.4.0/include/eigen3的内容替换到/usr/include/eigen3

将eigen3.4.0的/usr/share/pkg-config/eigen3.4.0.pc替换/usr/share/pkg-config/eigen3.pc

将eigen3.4.0的/usr/lib/cmake/eigen3.4.0/替换/usr/lib/cmake/eigen3/

(九)系统安装和源码安装的库

eigen

查询安装版本:

$ pkg-config --modversion eigen3
3.3.4

$ dpkg -l | grep eigen3
ii libeigen3-dev  3.3~beta1-2  all  lightweight C++ template library for linear algebra

lapack

查询安装版本:

$ dpkg -l | grep lapack
ii liblapack-dev  3.6.0-2ubuntu2  amd64  Library of linear algebra routines 3 - static version
ii liblapack3    3.6.0-2ubuntu2  amd64  Library of linear algebra routines 3 - shared version

blas

查询安装版本:

$ dpkg -l | grep blas
ii libblas-common  3.6.0-2ubuntu2  amd64  Dependency package for all BLAS implementations
ii libblas-dev    3.6.0-2ubuntu2  amd64  Basic Linear Algebra Subroutines 3, static library
ii libblas3     3.6.0-2ubuntu2  amd64  Basic Linear Algebra Reference implementations, shared library

suitesparse

查询安装版本:

$ dpkg -l | grep suitesparse
ii libsuitesparse-dev:amd64      1:4.4.6-1  amd64  libraries for sparse matrices computations (development files)
ii libsuitesparseconfig4.4.6:amd64  1:4.4.6-1  amd64  configuration routines for all SuiteSparse modules

gflags

查询安装版本:

$ dpkg -l | grep gflags
ii libgflags-dev  2.1.2-3  amd64  commandline flags module for C++ (development files)
ii libgflags2v5   2.1.2-3  amd64  commandline flags module for C++ (shared library)

glog

查询安装版本:

$ dpkg -l | grep glog
ii libgoogle-glog-dev  0.3.4-0.1  amd64  library that implements application-level logging.
ii libgoogle-glog0v5   0.3.4-0.1  amd64  library that implements application-level logging.

lua

查询安装版本:

$ dpkg -l | grep lua
ii liblua5.2-0:amd64   5.2.4-1ubuntu1  amd64  Shared library for the Lua interpreter version 5.2
ii liblua5.2-dev:amd64  5.2.4-1ubuntu1  amd64  Development files for the Lua language version 5.2

protobuf

查询安装版本:

$ dpkg -l | grep protobuf
ii libprotobuf-dev:amd64    2.6.1-1.3  amd64  protocol buffers C++ library (development files)
ii libprotobuf-lite9v5:amd64  2.6.1-1.3  amd64  protocol buffers C++ library (lite version)
ii libprotobuf9v5:amd64     2.6.1-1.3  amd64  protocol buffers C++ library
ii protobuf-compiler      2.6.1-1.3  amd64  compiler for protocol buffer definition files
⚠️ **GitHub.com Fallback** ⚠️