1. catkin_make 與cmake的關系
程序在cmake編譯是這樣的流程, cmake指令依據你的CMakeLists.txt 文件,生成makefiles文件,make再依據此makefiles文件編譯鏈接生成可執行文件. catkin_make是將cmake與make的編譯方式做了一個封裝的指令工具, 規范了工作路徑與生成文件路徑.
-
cmake標准流程
# 在一個CMake項目里
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install # (可選)
-
catkin_make 的流程
# In a catkin workspace $ catkin_make $ catkin_make install # (可選) 如果源碼不在默認工作空間,需要指定編譯路徑: # In a catkin workspace $ catkin_make --source my_src $ catkin_make install --source my_src # (optionally)
2 catkin_make
CMake coding standards && CMake Variables
-
catkin_make默認的路徑信息
在catkin_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 #catkin_make 封裝運行中cmake運行的情況 Running command: "cmake /home/user/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/home/user/catkin_ws/build" #編譯工具查找 -- Using CATKIN_DEVEL_PREFIX: /tmp/catkin_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/kinetic -- This workspace overlays: /opt/ros/kinetic #編譯的包 <pre name="code" class="cpp"> #catkin_make 封裝運行中make運行的情況
#### Running command: "make -j4" in "/home/user/catkin_ws/build"
-
layout :ros工作空間文件系統結構
workspace_folder/ --WORKSPACE src/ --SOURCE SPACE CMakeLists.txt/ --This is symlinked to catkin/cmake/toplevel.cmake package_1/ CMakeLists.txt package.xml ... package_n/ CMakeLists.txt package.xml build/ --BUILD SPACE(this is where build system is invoked, not necessarily within workspace) CATKIN_IGNORE --Marking the folder to be ignored when crawling for packages (necessary when source space is in the root of the workspace, the file is emtpy) #此選項可用於忽略某個包編譯 devel/ --DEVEL SPACE (targets go here, parameterizable, but defaults to peer of Build Space) # 生成二值 庫 可執行文件 bin/ etc/ /include/ lib/ share/ .catkin --Marking the folder as a development space (the file contains a semicolon separated list of Source space paths) # env.bash setup.bash setup.sh ... install/ --INSTALL SPACE (this is where installed targets for test installations go, not necessarily within workspace) bin/ etc/ include/ lib/ share/ .catkin --Marking the folder as an install space (the file is emtpy) env.bash setup.bash -- Environment setup file for Bash shell setup.sh -- Environment setup file for Bourne shell ... / opt/ ros/ kinetic/ setup.bash -- Environment setup file for Bash shell setup.sh -- Environment setup file for Bourne shell setup.zsh -- Environment setup file for zshell ...
工作空間
源空間
編譯空間
開發空間
安裝空間 -DCMAKE_INSTALL_PREFIX=/any/directory cmake默認是/usr/local
系統安裝空間 /opt/ros/ROSDISTRO
結果空間 source RESULT-SPACE/setup.sh #類似掃描安裝空間與開發空間,替換系統通用下的對應文件.
catkin支持包的逐層覆蓋, 當前最高,其它依據source的順序逐層變高, 高層可覆蓋低層.
Example 4: Overlaying workspace 3 on top of local workspace2 install space on top of workspace1 devel space on top of system install cd ~/workspace2/build cmake -DCMAKE_INSTALL_PREFIX=~/ws2_installed ../src make make install source ~/ws2_installed/setup.bash cd ~/workspace3/build cmake ../src make
ros 環境變量設置 可以參考 .bashrc文件: ros工作環境設置
#package slam_ws source /opt/ros/indigo/setup.bash source /home/yhzhao/slam_ws/devel/setup.bash export ROS_PACKAGE_PATH=~/slam_ws/src:$ROS_PACKAGE_PATH export ROS_WORKSPACE=~/slam_ws/src
- 混合 Mixing catkin And rosbuild Workspaces
catkin was designed to allow rosbuild packages to sit on top of catkin ones. This is accomplished by using overlays
~/rosbuild_ws/ dry_pkg1/ ... dry_pkgN/ setup.bash setup.sh ~/catkin_ws/ src/ wet_pkg1/ ... wet_pkgN/ build/ devel/ setup.bash setup.sh install/ setup.bash setup.sh
注: 我們在系統文件夾下的 .bashrc 中加入相應的source文件, 就是為了添加ros 的環境變量等信息.
Differences in CMakeLists.txt for rosbuild and catkin
-
catkin_make 編譯指定的包.
$ catkin_make -DCATKIN_WHITELIST_PACKAGES="package1;package2"
恢復編譯所有的包
$ catkin_make -DCATKIN_WHITELIST_PACKAGES=""
3 catkin
4 package. xml 與 CMakeList. txt
4.1 package. xml
每個包的描述文件,都需要放置在包的根目錄下,對包的名字/版本/作者/維護者/依賴關系進行說明.與rosbuild中的manifest.xml相似. 依賴不正確在本地可以可以編譯通過,但不能在ROS社區正確工作起來.
4.1.1 格式
<package> <name>foo_core</name> <version>1.2.4</version> <description> This package provides foo capability. </description> <maintainer email="ivana@willowgarage.com">Ivana Bildbotz</maintainer> <license>BSD</license> <url>http://ros.org/wiki/foo_core</url> <author>Ivana Bildbotz</author> <buildtool_depend>catkin</buildtool_depend> <depend>roscpp</depend> <depend>std_msgs</depend> <build_depend>message_generation</build_depend> <exec_depend>message_runtime</exec_depend> <exec_depend>rospy</exec_depend> <test_depend>python-mock</test_depend> <doc_depend>doxygen</doc_depend> </package>
C++ catkin library dependencies
6種依賴標簽
Build Dependencies #包編譯需要依賴的包.
Build Export Dependencies #指出你的包編譯導出庫 (???): b依賴a的頭文件,要想c只依賴b. 我認為是避免多次依賴.
specify which packages are needed to build libraries against this package. This is the case when you transitively include their headers in public headers in this package (especially when these packages are declared as (CATKIN_)DEPENDS in catkin_package() in CMake).
Execution Dependencies #執行時依賴
Test Dependencies #單元測試
Build Tool Dependencies #編譯系統工具
Documentation Tool Dependencies #doc生成工具
<depend> specifies that a dependency is a build, export, and execution dependency. This is the most commonly used dependency tag. <buildtool_depend> <build_depend> <build_export_depend> <exec_depend> <test_depend> <doc_depend>http://write.blog.csdn.net/postedit/50388429
4.1.2 Metapackages 將多個包組合成一個邏輯包
<export> <metapackage /> </export>
僅需要標簽: <buildtool_depends> 依賴 catkin 和 <run_depend>
對應cmakelists.txt中
cmake_minimum_required(VERSION 2.8.3) project(<PACKAGE_NAME>) find_package(catkin REQUIRED) catkin_metapackage()
例如 universal_robot的package.xml
<package> <name>universal_robot</name> <version>1.1.5</version> <description> Drivers, description, and utilities for Universal Robot Arms. </description> <maintainer email="aub@ipa.fhg.de">Alexander Bubeck</maintainer> <license>BSD</license> <url type="website">http://ros.org/wiki/universal_robot</url> <author email="sedwards@swri.org">Shaun Edwards</author> <author>Stuart Glaser</author> <author email="kphawkins@gatech.edu">Kelsey Hawkins</author> <author>Wim Meeussen</author> <author email="fxm@ipa.fhg.de">Felix Messmer</author> <buildtool_depend>catkin</buildtool_depend> <run_depend>ur3_moveit_config</run_depend> <run_depend>ur5_moveit_config</run_depend> <run_depend>ur10_moveit_config</run_depend> <run_depend>ur_bringup</run_depend> <run_depend>ur_description</run_depend> <run_depend>ur_driver</run_depend> <run_depend>ur_gazebo</run_depend> <run_depend>ur_kinematics</run_depend> <run_depend>ur_msgs</run_depend> <export> <metapackage/> </export> </package>
cmake_minimum_required(VERSION 2.8.3) project(universal_robot) find_package(catkin REQUIRED) catkin_metapackage()
4.2 CMakeLists.txtcmak 不會找package.xml文件, 但catkin需要. 依據cmakelists.txt文件編譯需要清晰指出頭文件和庫文件的指向.
Finding the library
find_package(catkin REQUIRED COMPONENTS roscpp)
Include directories
include_directories(include ${catkin_INCLUDE_DIRS})
Exporting interfaces
需要在package.xml中使用<depend> 或者<build_export_depend>,catkin_package() 命令僅調用一次,它需要額外的參數依賴於你的包導出的依賴.
catkin_package(CATKIN_DEPENDS angles roscpp std_msgs)
4.3 C++ 系統庫依賴
Finding the library
4.3.1 依據CMake module . 大部分的boost 庫在頭文件完全實現,運行時不需要獨立的分享連接
find_package(Boost REQUIRED)
但boost thread 運行時需要庫,必須指出組件thread .
find_package(Boost REQUIRED COMPONENTS thread)
find_package()之后就可以用此編譯和連接了. 命名規則基本是這樣: ${name}_INCLUDE_DIRS and${name}_LIBRARIES .有時不遵循
4.3.2 cmake modules 無效情況下,但庫開發包提供了pkg-config module . 編譯標簽
find_package(PkgConfig REQUIRED) pkg_check_modules(GSTREAMER REQUIRED libgstreamer-0.10)
pkg_check_modules() 參數聲明了一個cmake前綴,GSTREAMER_INCLUDE_DIRS 和GSTREAMER_LIBRARIES
PkgConfig 編譯應用與連接的幫助工具. 用於命令行中插入正確的編譯選項. (.pc文件 ) libgstreamer-0.10.pc.
Include directories
include_directories(include ${Boost_INCLUDE_DIRS} ${GSTREAMER_INCLUDE_DIRS})
如有依賴catkin 包 ,再添加 ${catkin_INCLUDE_DIRS}
Exporting interfaces
catkin_package(DEPENDS Boost GSTREAMER)
確保所有的包在package.xml被用到, <build_export_depend> 和<depend>標簽.
標准命名 ${name}_INCLUDE_DIRS and${name}_LIBRARIES . catkin包一般是小寫,大寫(asGSTREAMER) 或混合 (likeBoost)
4.4 編譯安裝 C++ libraries and headers
include_directories(include ${catkin_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${GSTREAMER_INCLUDE_DIRS}) add_library(your_library libsrc1.cpp libsrc2.cpp libsrc_etc.cpp) set(YOUR_LIB_SOURCES libsrc1.cpp libsrc2.cpp libsrc3.cpp libsrc4.cpp libsrc_etc.cpp) add_library(your_library ${YOUR_LIB_SOURCES}) target_link_libraries(your_library ${catkin_LIBRARIES}) target_link_libraries(your_library ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${GSTREAMER_LIBRARIES}) catkin_package(CATKIN_DEPENDS std_msgs DEPENDS Boost INCLUDE_DIRS include LIBRARIES your_library) <depend>std_msgs</depend> <build_export_depend>boost</build_export_depend>
安裝
install(TARGETS your_library your_other_library ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}) install(DIRECTORY include/${PROJECT_NAME}/ DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}) install(DIRECTORY include/${PROJECT_NAME}/ DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} PATTERN ".svn" EXCLUDE)
4.5 編譯安裝 C++ 執行文件
include_directories(include ${catkin_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${GSTREAMER_INCLUDE_DIRS}) add_executable(your_node src1.cpp src2.cpp src_etc.cpp) set(${PROJECT_NAME}_SOURCES src/file1.cpp src/file2.cpp src/file3.cpp src/file4.cpp src/file5.cpp src/file6.cpp src/file_etc.cpp) add_executable(your_node ${${PROJECT_NAME}_SOURCES}) add_executable(your_node ${${PROJECT_NAME}_SOURCES}) target_link_libraries(your_node ${catkin_LIBRARIES}) add_executable(your_node ${${PROJECT_NAME}_SOURCES}) target_link_libraries(your_node ${catkin_LIBRARIES} ${Boost_LIBRARIES} ${GSTREAMER_LIBRARIES})
ros下的安裝,使得rosrun 與roslaunch能夠使用.需要下面的方式安裝
install(TARGETS your_node
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
安裝 cmake文件
catkin_package(CFG_EXTRAS your_macros.cmake your_modules.cmake) install(FILES cmake/your_macros.cmake cmake/your_modules.cmake DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/cmake) install(DIRECTORY cmake DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} PATTERN ".svn" EXCLUDE)
4.6 CMake coding standards
4.7 CMake Variables
參考:
http://wiki.ros.org/catkin
http://wiki.ros.org/catkin/commands/catkin_make
http://wiki.ros.org/cn/ROS/Tutorials/catkin/BuildingPackages