編譯ROS程序包(四)

本教程介紹ROS程序包的編譯方法

內容

編譯程序包
    使用 catkin_make
    開始編譯你的程序包

編譯程序包

一旦安裝了所需的系統依賴項,我們就可以開始編譯剛纔創建的程序包了。

注意: 如果你是通過apt或者其它軟件包管理工具來安裝ROS的,那麼系統已經默認安裝好所有依賴項。

記得事先source你的環境配置(setup)文件,在Ubuntu中的操作指令如下:

$ source /opt/ros/groovy/setup.bash

使用 catkin_make

catkin_make 是一個命令行工具,它簡化了catkin的標準工作流程。你可以認爲catkin_make是在CMake標準工作流程中依次調用了cmake 和 make。

使用方法:

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

CMake標準工作流程主要可以分爲以下幾個步驟:

注意: 如果你運行以下命令是無效的,因爲它只是一個演示CMake工作流程的例子。

# 在一個CMake項目裏
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install  # (可選)

每個CMake工程在編譯時都會執行這個操作過程。相反,多個catkin項目可以放在工作空間中一起編譯,工作流程如下:

# In a catkin workspace
$ catkin_make
$ catkin_make install  # (可選)

上述命令會編譯src文件夾下的所有catkin工程。想更深入瞭解請參考REP128。 如果你的源代碼不在默認工作空間中(~/catkin_ws/src),比如說存放在了my_src中,那麼你可以這樣來使用catkin_make:

注意: 運行以下命令時無效的,因爲my_src不存在。

# In a catkin workspace
$ catkin_make --source my_src
$ catkin_make install --source my_src  # (optionally)

對於catkin_make更高級的使用方法,請參考catkin/commands/catkin_make

開始編譯你的程序包

對於正要馬上編譯自己代碼的讀者,請同時看一下後面的(C++)/(Python)教程,因爲你可能需要修改CMakeLists.txt文件。

按照之前的創建一個ROS程序包教程,你應該已經創建好了一個catkin 工作空間 和一個名爲beginner_tutorials的catkin 程序包。現在切換到catkin workspace 並查看src文件夾:

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

你可以看到一個名爲beginner_tutorials的文件夾,這就是你在之前的 catkin_create_pkg教程裏創建的。現在我們可以使用catkin_make來編譯它了:

$ 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/groovy
    -- This workspace overlays: /opt/ros/groovy
    -- 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/workspaces。需要注意的是由於這些空間存在默認配置的原因,有幾個文件夾已經在catkin工作空間自動生成了,使用ls查看:

$ ls
build
devel
src

build 目錄是build space的默認所在位置,同時cmake 和 make也是在這裏被調用來配置並編譯你的程序包。devel 目錄是devel space的默認所在位置, 同時也是在你安裝程序包之前存放可執行文件和庫文件的地方。

現在我們已成功編譯了一個ROS程序包,接下來我們將介紹ROS節點.

原文地址:http://wiki.ros.org/cn/ROS/Tutorials/BuildingPackages

發佈了112 篇原創文章 · 獲贊 162 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章