指的是一种特定的文件结构和文件夹组合。通常将实现同一个具体功能的程序代码放到一个package中,比如实现相机数据采集这一功能
通常ROS文件组织都是按照以上的形式,这是约定俗成的命名习惯,建议遵守。以上路径中,只有 CMakeLists.txt 和 package.xml 是必须的,其余路径根据软件包是否需要来决定
cmake_minimum_required
)project()
)find_package()
)catkin_python_setup()
)add_message_files()
,add_service_files()
,add_action_files()
)generate_messages()
)catkin_package()
)add_library()
/ add_executable()
/ target_link_libraries()
)(target_link_libraryies
通常情况下要有)catkin_add_gtest()
)install()
)代码解析:
cmake_minimum_required(VERSION 2.8.3)
catkinCMakeLists.txt都要以此开始,catkin编译需要2.8.3版本以上的cmakeproject(beginner_tutorials)
通过project()
这个函数指定包的名字,在CMake
中指定后,你可在其他地方通过使用变量${PROJECT_NAME}
来引用它find_package
依赖功能包
这里指明构建这个package需要依赖的package,我们使用
catkin_make
的编译方式,至少需要catkin这个包,即find_package(catkin REQUIRED)
如果要使用 C++ 和 Boost,则需要引用
find_package
包含 Boost,并且指明 Boost 的类型,如使用 Boost threads,则:find_package(Boost REQUIRED COMPONENTS thread)
需要的所有包我们都可用这种方式包含进来,比如我们还需要roscpp,rospy,std_msgs。我们可以写成:
find_package(roscpp REQUIRED)
find_package(rospy REQUIRED)
find_package(std_msgs REQUIRED)
这样的话,每个依赖的package都会产生几个变量,这样很不方便。所以还有另外一种方式:
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation )
这样,它会把所有pacakge里面的头文件和库文件等等目录加到一组变量上,比如:catkin_INCLUDE_DIRS
,这样,我们就可以用这个变量查找需要的文件了。最终就只产生一组变量了Declare ROS messages, services and actions
- 当我们需要使用.msg .srv .action形式的文件时,我们需要特殊的预处理器把他们转化为系统可以识别特定编程语言(.h .cpp)。系统会用里面所有的(一些编程语言)生成器(比如 gencpp, genpy, genlisp, etc)生成相应的.cpp .py文件
- 这就需要三个宏:
add_message_files
,add_service_files
,add_action_files
来相应的控制.msg .srv .action
。这些宏后面必须跟着一个调用generate_messages()
- 这些宏必须在
catkin_package()
宏前面find_package()
必须依赖包message_generation
- package.xml文件
build_depend
必须包含message_generation
,run_depend
必须包含message_runtime
如果你有一个包编译.msg .srv
,并且可执行文件要使用他们,那么你就需要创建一个显式的依赖项,自动生成message
的target
。这样才能按顺序来进行编译:
add_dependencies(some_target ${PROJECT_NAME}_generate_messages_cpp)
(这里的some_target
是add_executable()
设置的target
的名字)catkin_package()
这是一个catkin提供的cmake
宏,当我们要给构建系统指定catkin的特定的信息时就需要了,或者反过来利用他产生pkg-config和CMake文件。这个函数必须在声明add_library()
或者add_executable()
生成target之前使用,该函数有5个可选参数:
INCLUDE_DIRS
:包的导出包含路径LIBRARIES
:从项目导出的库CATKIN_DEPENDS
:该项目依赖的其他catkin项目DEPENDS
:该项目所依赖的非catkin CMake项目。为了更好的理解,请看这个解释。CFG_EXTRAS
:其他配置选项
Specifying Build Targets
(指定编译的target)
编译产生的target有多种形式,通常有两种:程序可以运行的可执行文件以及在可执行文件编译和运行时要用到的库
- target的命名
target的命名很重要,在catkin中target的名字必须是唯一的,和你之前构建产生的和安装的都不能相同。这只是cmake内部的需要。可以利用set_target_properties()
函数将以个target进行重命名,例如:
set_target_properties(rviz_image_view PROPERTIES OUTPUT_NAME image_view PREFIX "")
这样就可将那个target rviz_image_view 改为image_view几个函数介绍:
include_directories
- 他的参数是通过
find_package
产生的*_INCLUDE_DIRS
变量和其他所有额外的头文件路径
例如:include_directories(include ${Boost_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})
(这里include
表示你的pacakge里面的include
这个路径也包含在里面)link_directories()
- 这个函数用来添加额外的库的路径,然而,这并不鼓励使用,因为所有的catkin和cmake的package在使用
find_package
时就已经自动的有他们的链接信息- 简单的连接可以通过
target_link_libraries()
来进行Executable Targets
(可执行target)
- 要指定必须构建的可执行目标,我们必须使用
add_executable()
- 例如:
add_executable(myProgram src/main.cpp src/some_file.cpp src/another_file.cpp)
- 库target
add_library()
用来指定编译产生的库- 默认的catkin编译产生共享库:
add_library(${PROJECT_NAME} ${${PROJECT_NAME}_SRCS})
- 链接库(
target_link_libraries
)
- 使用
target_link_libraries
函数来指定可执行文件链接的库。- 这个要用在
add_executable()
后面- 使用
target_link_libraries(
, , , ... )
其他配置说明:
- 启用Python模块支持
- 如果您的ROS包提供了一些Python模块,您应该创建一个setup.py文件并调用
catkin_python_setup()
- 单元测试
- 有一个
catkin-specific
宏用于处理名为catkin_add_gtest()
的基于gtest的单元测试catkin_add_gtest(myUnitTest test / utest.cpp)
- 指定可安装的目标
构建时间之后,目标被放置在catkin工作空间的空间中。但是,通常我们希望将目标安装到系统中(有关安装路径的信息可以在REP 122中找到 ),以便其他人或本地文件夹可以使用它们来测试系统级安装。换句话说,如果你想要做一个“make install”的代码,你需要指定目标应该在哪里
- 这是使用
CMake install()
函数作为参数完成的- 目标:目标是安装
ARCHIVE DESTINATION
:静态库和DLL(Windows).lib存根LIBRARY DESTINATION
:非DLL共享库和模块RUNTIME DESTINATION
:可执行目标和DLL(Windows)样式共享库
launch/
路径中msg/ , srv/ , action/
路径下param/
路径下# 显示rospack的用法
rospack help
# 列出本机所有package
rospack list
# 显示package的依赖包
rospack depends [package]
# 定位某个package
rospack find [package]
# 刷新所有package的位置记录
rospack profile
ddu@ddu-virtual-machine:~/catkin_ws/src$ cd ~/catkin_ws/src
ddu@ddu-virtual-machine:~/catkin_ws/src$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
# 查看依赖包情况
ddu@ddu-virtual-machine:~/catkin_ws/src$ rospack depends1 beginner_tutorials
ddu@ddu-virtual-machine:~/catkin_ws/src/beginner_tutorials$ tree
.
├── CMakeLists.txt
├── include
│ └── beginner_tutorials
├── package.xml
└── src
3 directories, 2 files
ddu@ddu-virtual-machine:~/catkin_ws/src/beginner_tutorials$ cat CMakeLists.txt
# catkin编译需要2.8.3版本以上的cmake
cmake_minimum_required(VERSION 2.8.3)
# 通过project()这个函数指定包的名字,在CMake中指定后,你可在其他地方通过使用变量${PROJECT_NAME}来引用它
project(beginner_tutorials)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
# 这里指明构建这个package需要依赖的package,我们使用catkin_make的编译方式,至少需要catkin这个包
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
# 也可以分开写:
# find_package(roscpp REQUIRED)
# find_package(rospy REQUIRED)
# find_package(std_msgs REQUIRED)
# 如果要使用 C++ 和 Boost,则需要引用 find_package 包含 Boost,并且指明 Boost 的类型,如使用 Boost threads,则:
# find_package(Boost REQUIRED COMPONENTS thread)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
# 如果您的ROS包提供了一些Python模块,您应该创建一个setup.py文件并调用
# 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(catkin REQUIRED COMPONENTS ...)
## * 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
# Action1.action
# Action2.action
# )
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# 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提供的cmake宏,当我们要给构建系统指定catkin的特定的信息时就需要了,或者反过来利用他产生pkg-config和CMake文件。
## 这个函数必须在声明add_library()或者 add_executable()生成target之前使用,该函数有5个可选参数:
## INCLUDE_DIRS: 包的导出包含路径
## LIBRARIES: 从项目导出的库
## CATKIN_DEPENDS: 该项目依赖的其他catkin项目
## DEPENDS: 该项目所依赖的非catkin CMake项目。为了更好的理解,请看这个解释
## CFG_EXTRAS: 其他配置选项
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES beginner_tutorials
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/beginner_tutorials.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/beginner_tutorials_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 for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_GLOBAL_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_beginner_tutorials.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)
...
ddu@ddu-virtual-machine:~/catkin_ws/src/beginner_tutorials$ cat package.xml
<?xml version="1.0"?>
<package format="2">
<name>beginner_tutorials</name>
<version>0.0.0</version>
<description>The beginner_tutorials package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="[email protected]">ddu</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/beginner_tutorials</url> -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="[email protected]">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>