说明:本文主要是结合 ROS 官网的相关内容以及自己的相应操作心得整理而成,记录在此处仅仅是为了自己查找方便!
ROS 官网相关参考内容网址:http://wiki.ros.org/ROS/Tutorials/BuildingPackages
1. build packages
只要安装了 package 的所有系统依赖,就可以 build 新的 package。开始之前先 source 环境配置文件:
source /opt/ros/melodic/setup.bash
1.1 使用 catkin_make
catkin_make 是一个命令行工具,它为标准的 catkin 工作流程增加了一些便利。 可以想象 catkin_make 在标准 CMake 工作流程中组合了对 cmake 和 make 的调用。
应用示例:
# In a catkin workspace
catkin_make [make_targets] [-DCMAKE_VARIABLES=...]
对于不熟悉标准 CMake 工作流程的人,它分解如下:
# In a CMake project
mkdir build
cd build
cmake ..
make
make install # (optionally)
每个 CMake 项目都会运行此过程。 相比之下,catkin 项目可以在工作空间中一起 build。 在工作区中 build 零到多个 catkin package 遵循以下工作流程:
# In a catkin workspace
catkin_make
catkin_make install # (optionally)
上述命令将 build 在 src 文件夹中找到的任何 catkin 项目, 这遵循 REP128 设定的建议。 如果源代码在不同的地方,比如 my_src 那么你会像这样调用 catkin_make:
# In a catkin workspace
catkin_make --source my_src
catkin_make install --source my_src # (optionally)
有关 catkin_make 的更高级用法,请参阅文档:http://wiki.ros.org/catkin/commands/catkin_make
1.2 build packages
如果正在使用此页面构建自己的代码,请同时查看教程 (C++)/(Python),因为可能需要修改 CMakeLists.txt。
现在应该已经有了一个 catkin 工作区和一个新的叫做 begin_tutorials 的 catkin package。 如果您不在那里,请进入 catkin 工作区并查看 src 文件夹:
我们现在开始使用 catkin_make 来 build package:
catkin_make
可以看到许多来自 cmake 和 make 的输出,类似:
Base path: /home/user/catkin_ws
Source space: /home/user/catkin_ws/src
Build space: /home/user/catkin_ws/build
Devel space: /home/user/catkin_ws/devel
Install space: /home/user/catkin_ws/install
####
#### Running command: "cmake /home/user/catkin_ws/src
-DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel
-DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install" in "/home/user/catkin_ws/build"
####
-- The C compiler identification is GNU 4.2.1
-- The CXX compiler identification is Clang 4.0.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Using CATKIN_DEVEL_PREFIX: /tmp/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/kinetic
-- This workspace overlays: /opt/ros/kinetic
-- Found PythonInterp: /usr/bin/python (found version "2.7.1")
-- Found PY_em: /usr/lib/python2.7/dist-packages/em.pyc
-- Found gtest: gtests will be built
-- catkin 0.5.51
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing packages in topological order:
-- ~~ - beginner_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ add_subdirectory(beginner_tutorials)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/catkin_ws/build
####
#### Running command: "make -j4" in "/home/user/catkin_ws/build"
####
请注意, catkin_make 首先显示它用于每个“空间”的路径。 这些空间在 REP128 和维基上关于 catkin 工作区的文档中进行了描述:catkin/workspaces。 需要注意的重要一点是,catkin 工作区中已经默认创建了几个文件夹。 用 ls 看看:
build 文件夹是 build 空间的默认位置,是调用 cmake 和 make 来配置和 build 你的 package 的地方。 devel 文件夹是 devel 空间的默认位置,这是安装 package 之前可执行文件和库所在的位置。