Note: This tutorial assumes that you have completed the previous tutorials: 创建ROS软件包.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

构建ROS软件包

Description: 本教程介绍了构建软件包及使用的工具链。

Tutorial Level: BEGINNER

Next Tutorial: 理解ROS节点

Building Packages

一旦安装了所需的系统依赖项,我们就可以编译刚刚创建的package了。

使用rosmake

rosmake是跟make 类似的小工具, 但它会针对ROS做一些相应的处理。当你执行rosmake beginner_tutorials命令, 编译beginner_tutorials package,并按照正确的顺序链接所依赖的package。 因为我们在创建的ROS package的时候加入了对 rospy, roscpp, 和std_msgs 的依赖,所以这些package(以及它们的依赖项,等等,如此反复) 都将会被rosmake所编译。

使用方法:

rosmake [package]

执行:

$ rosmake beginner_tutorials

这条指令的执行需要一些时间。在这期间,你会看到如下的信息输出:

  • [ rosmake ] No package specified.  Building ['beginner_tutorials']
    [ rosmake ] Logging to directory
    [ rosmake ] /home/dbking/.ros/rosmake_output-2009-09-22-03-17-14
    [ rosmake ] [ 0 of 18  Completed ]
    [rosmake-0] >>> genmsg_cpp >>> [ make ]
    [rosmake-0] <<< genmsg_cpp <<< [PASS] [ 0.39 seconds ]
    [ rosmake ] [ 1 of 18  Completed ]
    ...
    ...
    ...
    [ rosmake ] [ 17 of 18  Completed ]
    [rosmake-0] >>> beginner_tutorials >>> [ make ]
    [rosmake-0] <<< beginner_tutorials <<< [PASS] [ 0.79 seconds ]

在Fuerte版本, 依赖项被大规模精简,所以几乎会马上输出:

  • [ rosmake ] rosmake starting...                                                                     
    [ rosmake ] Packages requested are: ['beginner_tutorials']                                          
    [ rosmake ] Logging to directory /home/alex/.ros/rosmake/rosmake_output-20120603-082414             
    [ rosmake ] Expanded args ['beginner_tutorials'] to:
    ['beginner_tutorials']                         
    [rosmake-0] Starting >>> std_msgs [ make ]                                                          
    [rosmake-1] Starting >>> roslang [ make ]                                                           
    [rosmake-0] Finished <<< std_msgs ROS_NOBUILD in package std_msgs
     No Makefile in package std_msgs  
    [rosmake-1] Finished <<< roslang ROS_NOBUILD in package roslang
     No Makefile in package roslang     
    [rosmake-1] Starting >>> rospy [ make ]                                                             
    [rosmake-2] Starting >>> roscpp [ make ]                                                            
    [rosmake-1] Finished <<< rospy ROS_NOBUILD in package rospy
     No Makefile in package rospy           
    [rosmake-2] Finished <<< roscpp ROS_NOBUILD in package roscpp
     No Makefile in package roscpp        
    [rosmake-2] Starting >>> beginner_tutorials [ make ]                                                
    [rosmake-2] Finished <<< beginner_tutorials [PASS] [ 1.14 seconds ]                                 
    [ rosmake ] Results:                                                                                
    [ rosmake ] Built 5 packages with 0 failures.                                                       
    [ rosmake ] Summary output to directory                                                             
    [ rosmake ] /home/alex/.ros/rosmake/rosmake_output-20120603-082414  

rosmake 多个ROS package

同样,我们可以利用 rosmake同时编译多个package。

使用方法:

rosmake [package1] [package2] [package3]

Review

下面,复习一下刚刚使用过的指令:

  • rosdep = ros+dep(endencies) : 安装package所需系统依赖项的小工具
  • rosmake = ros+make : 编译 ROS package

构建软件包

只要安装了这个包的所有系统依赖项,就可以开始编译软件包了。

注意:如果你是通过apt或者其它软件包管理器来安装ROS的,那么系统已经安装好了所有的依赖项。

如果你还没设置环境的话,记得先soucre一下。在Ubuntu中的操作如下:

$ source /opt/ros/<distro>/setup.bash

使用catkin_make

catkin_make 是一个命令行工具,它简化了标准catkin工作流程。你可以认为catkin_make是在标准CMake工作流程中依次调用了cmakemake

用法:

# 在catkin工作空间下
$ catkin_make [make_targets] [-DCMAKE_VARIABLES=...]

如果你不熟悉什么是标准CMake工作流程,可以认为是以下几个步骤:

注意:直接运行以下命令是无效的,因为它只是一个演示CMake工作流程的例子。

# 在CMake工作空间下
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install  # (可选)

每个CMake项目都要单独进行这样的步骤。相反,多个catkin项目可以放在工作空间中一起构建,在工作空间中构建零到多个catkin软件包为以下工作流程:

# 在catkin工作空间下
$ catkin_make
$ catkin_make install  # (可选)

上述命令会构建src目录下的所有catkin项目。该过程遵循REP128的建议。如果你的源代码不在默认位置(catkin_ws/src),比如说存放在了my_src中,那可以这样来使用catkin_make:

注意:直接运行以下命令是无效的,因为my_src可能不存在。

# 在catkin工作空间下
$ catkin_make --source my_src
$ catkin_make install --source my_src  # (可选)

对于catkin_make的高级用法,请参考catkin/commands/catkin_make

开始构建你的软件包

如果现在就要构建自己的代码,请同时看一下后面的(C++)/(Python)教程,因为你可能需要修改CMakeLists.txt文件。

按照之前的创建ROS软件包教程,你应该已经创建好了一个catkin工作空间和一个名为beginner_tutorials的catkin软件包。现在切换到catkin工作空间并查看src目录:

$ cd ~/catkin_ws/
$ ls src
  • beginner_tutorials/  CMakeLists.txt@  

你可以看到一个名为beginner_tutorials的目录,这就是你之前用catkin_create_pkg命令创建的。现在我们可以使用catkin_make来构建它了:

$ catkin_make

你可以看到很多cmakemake的输出信息:

  • 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首先输出它所使用到的每个空间所在的路径。更多关于空间的信息,请参考REP128catkin/workspaces。需要注意的是,根据这些变量的默认值,有几个目录已经在catkin工作空间中自动生成了,用ls看看:

$ ls
  • build
    devel
    src

build 目录是构建空间的默认位置,同时cmakemake也是在这里被调用来配置和构建你的软件包。而devel目录是开发空间的默认位置, 在安装软件包之前,这里可以存放可执行文件和库。

现在我们已成功构建了一个ROS软件包,让我们再聊聊理解ROS节点

Wiki: cn/ROS/Tutorials/BuildingPackages (last edited 2020-12-22 02:14:53 by yakamoz423)