声明:摘自MOOC 机器人操作系统入门 ros-tutorial-icourse163
Table of Contents
4.action_demo 的CMakeLists.txt实例
cmake是编译工具,catkin是rOS的编译系统.
1.catin的特点
一个Catkin的软件包( package) 必须要包括两个文件:
- package.xml: 包括了package的描述信息
- name, description, version, maintainer(s), license
- opt. authors, url's, dependencies, plugins, etc...
- CMakeLists.txt: 构建package所需的CMake文件
- 调用Catkin的函数/宏
- 解析 package.xml
- 找到其他依赖的catkin软件包
- 将本软件包添加到环境变量
2.catkin编译的工作原理:
catkin编译的工作流程如下:
1. 首先在工作空间 catkin_ws/src/ 下递归的查找其中每一个ROS的package。
2. package中会有 package.xml 和 CMakeLists.txt 文件, Catkin(CMake)编译系统依
据 CMakeLists.txt 文件,从而生成 makefiles (放在 catkin_ws/build/ )。
3. 然后 make 刚刚生成的 makefiles 等文件, 编译链接生成可执行文件(放在 catkin_ws/devel )。
也就是说, Catkin就是将 cmake 与 make 指令做了一个封装从而完成整个编译过程的工具。
catkin有比较突出的优点, 主要是:
- 操作更加简单
- 一次配置, 多次使用
- 跨依赖项目编译
3.CMakelist.txt整体结构
cmake_minimum_required() #CMake的版本号
project() #项目名称
find_package() #找到编译需要的其他CMake/Catkin package
catkin_python_setup() #catkin新加宏, 打开catkin的Python Module的支持
add_message_files() #catkin新加宏, 添加自定义Message/Service/Action文件
add_service_files()
add_action_files()
generate_message() #catkin新加宏, 生成不同语言版本的msg/srv/action接口
catkin_package() #catkin新加宏, 生成当前package的cmake配置, 供依赖本包的其他软件包调用
add_library() #生成库
add_executable() #生成可执行二进制文件
add_dependencies() #定义目标文件依赖于其他目标文件, 确保其他目标已被构建
target_link_libraries() #链接
catkin_add_gtest() #catkin新加宏, 生成测试
install() #安装至本机
4.action_demo 的CMakeLists.txt实例
cmake_minimum_required(VERSION 2.8.3)
project(action_lin)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
actionlib
actionlib_msgs
roscpp
message_generation
genmsg
)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
#find_package() 找到编译需要的其他CMake/Catkin package
#cmake宏, 指定依赖的其他pacakge, 实际是生成了一些环境变量, 如<NAME>_FOUND, <NAME>_INCLUDE_DIRS
## , <NAME>_LIBRARYIS
#此处catkin是必备依赖 其余的genmsg...为组件
find_package(catkin REQUIRED COMPONENTS
roscpp
message_generation
genmsg
actionlib_msgs
actionlib
)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )
## Generate actions in the 'action' folder
add_action_files(FILES DoDishes.action
)
## Generate added messages and services with any dependencies listed here
##用于生成action头文件/module,生成的文件位于devel/include当中
generate_messages(DEPENDENCIES
actionlib_msgs
std_msgs
)
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )
###################################
## catkin specific configuration ##
#catkin新加宏, 生成当前package的cmake配置, 供依赖本包的其他软件包调用
# catkin宏命令, 用于配置ROS的package配置文件和CMake文件
# 这个命令必须在add_library()或者add_executable()之前调用, 该函数有5个可选参数:
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
#- 该项目依赖的其他catkin项目
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
CATKIN_DEPENDS
message_runtime
# INCLUDE_DIRS include
# LIBRARIES action_lin
# CATKIN_DEPENDS other_catkin_pkg
# DEPENDS system_lib
)
###########
## Build ##
###########
##指定C++的头文件路径
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
include ${catkin_INCLUDE_DIRS}
# include
# ${catkin_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/action_lin.cpp
# )
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/action_lin_node.cpp)
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
# ${catkin_LIBRARIES}
# )
#############
## Install ##
#############
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# install(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark executables and/or libraries for installation
# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
#############
## Testing ##
#############
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_action_lin.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
# add_executable() #生成可执行二进制文件
# add_dependencies() #定义目标文件依赖于其他目标文件, 确保其他目标已被构建
# target_link_libraries() #链接可执行文件
add_executable(dodishes_clien_lin2
src/dodishes_client_lin2.cpp
)
add_dependencies(dodishes_clien_lin2 ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(dodishes_clien_lin2
${catkin_LIBRARIES}
)
add_executable(dodishes_server_lin2
src/dodishes_server_lin2.cpp
)
add_dependencies(dodishes_server_lin2 ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(dodishes_server_lin2
${catkin_LIBRARIES}
)