最近在群里啊社区啊老是看到学习ROS的搞到package.xml和CMakeList.txt文件的配置问题,好多其实自己可以解决的,但是因为看不到这两个文件,导致自己无从下手,我这里通过查阅网上的资料和自己的一些理解,整体汇总了一下内容,希望对大家有所帮助。可能存在错误,希望大家踊跃指出,共同进步!

关于package.xml文件的讲解

package.xml是ROS功能包的“功能包清单描述”,这文件指出了当前功能包的一些信息,比如名称啊、版本啊、描述啊、作者信息等等,最重要的是声明了编译工具、编译依赖工具、编译输出依赖和运行依赖。我们通过解读这个文件可以描述功能包的属性,包括功能包的名字、版本号、作者、维护者、通行证以及所以来的功能包。
我们这里先来看一下package.xml的原始文件,这里我以一个名为learning_cplus功能包的package.xml做出介绍。
<?xml version="1.0"?>
<!-- 当前xml版本 -->

<package format="2">
  
   <name>learning_cplus</name>
   <!-- 功能包名称是learning_cplus,name,名称 -->
  
   <version>0.0.0</version>
  <!-- 当前功能包版本是0.0.0,version,版本--> 
  
  <description>The learning_cplus package</description>
  <!-- 当前功能包介绍,description,详细 -->
  
  <maintainer email="waveshare@todo.todo">waveshare</maintainer>
  <!-- 当前维护者者信息(邮箱和昵称)-->

  <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
  <license>TODO</license>
  <!-- 协议版本,默认TODO就可以。有兴趣可以自行了解。-->

  <!-- <url type="website">http://wiki.ros.org/learning_cplus</url> -->
  <!-- 可共享路径,你可以把你的这个包上传到ROS社区或者github。-->

  <author email="jane.doe@example.com">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>
  <!--编译工具是catkin -->
  
  <build_depend>roscpp</build_depend>
  <!--编译依赖roscpp -->
  <build_depend>rospy</build_depend>
  <!--编译依赖rospy -->
  <build_depend>std_msgs</build_depend>
  <!--编译依赖std_msgs -->
  
  <build_export_depend>roscpp</build_export_depend>
  <!--编译输出依赖roscpp -->
  <build_export_depend>rospy</build_export_depend>
  <!--编译输出依赖rospy -->
  <build_export_depend>std_msgs</build_export_depend>
  <!--编译输出依赖std_msgs -->
  
  <exec_depend>roscpp</exec_depend>
  <!--运行依赖roscpp -->
  <exec_depend>rospy</exec_depend>
  <!--运行依赖rospy -->
  <exec_depend>std_msgs</exec_depend>
  <!--运行依赖std_msgs -->


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->
    <!-- 输出 -->
  </export>
</package>

关于CMakeList.txt文件的讲解

CMakeList.txt文件是CMake编译系统编译软件包过程的输入文件。任何CMake兼容包都包含一个或多个CMakeLists.txt文件,这些文件描述了如何编译代码以及将其安装到哪里。将CMakeLists.txt文件应用于一个catkin项目时,它就作为一个标准的附带一些限制条件的vanilla CMakeLists.txt文件。使用CMake编译程序时,cmake指令依据CMakeLists.txt 文件生成makefiles文件,make命令再依据makefiles文件编译链接生成可执行文件。
catkin是ROS官方的一个编译构建系统,是原本的ROS的编译构建系统rosbuild的发展。catkin_make是将cmake与make的编译方式做了一个封装的指令工具,规范了工作路径与生成文件路径。
CMakeList.txt的一个总体结构和顺序如下。
必需的CMake版本:cmake_minimum_required()
cmake_minimum_required(VERSION 3.0.2)
软件包名:project()
project(learning_cplus)
查找编译依赖的其他CMake/Catkin包(声明依赖库):find_package()
## 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
  roscpp
  rospy
  std_msgs
)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
启动Python模块支持:catkin_python_package()
## 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()
消息/服务/操作(Message/Service/Action)生成器:add_message_files(),add_service_files(),add_action_files()
################################################
## 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_messages()
## Generate added messages and services with any dependencies listed here
# generate_messages(
#   DEPENDENCIES
#   std_msgs
# )
动态重新配置参数:generate_dynamic_reconfigure_options()
################################################
## 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_package()
###################################
## catkin specific configuration ##
###################################
## 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
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES learning_cplus
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)
添加要编译的库和可执行文件:add_library()/add_executable()/target_link_libraries()
###########
## 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}/learning_cplus.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/learning_cplus_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()
#############
## 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
# catkin_install_python(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}
# )
测试编译:catkin_add_gtest()
#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_learning_cplus.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)
简单的说,首先是CMake版本,这玩意是我们的编译工具首先做声明。
接下来是我们这个功能包的名字。
第三是编译依赖项,我们的这个包编译的时候,需要依赖哪些文件什么的。
第四是Python编译模块的,如果你没有Python的全局脚本,这玩意就不需要。
第五是消息、服务、动作三大生成,这三个也是我们ROS系统的一个核心。
第六是调用了哪些外部的消息包什么的(后面会有详细介绍)。
第七是我们的cfg配置文件,配置一些rviz格式啊,记录一些配置参数啊等等。
第八是我们的以个编译输出配置,都依赖什么,要输出什么,在这个地方会记录的清清楚楚。
第九是我们需要编译的的代码和编译时候要用到的库,这两个需要给连接起来。
第十就是我们的一个按照规则是什么样的。
最后就是我们的测试编译配置。
这是一个CMakeList.txt模板文件的一个内容,我们在实际做的时候用到的写,没用的可以直接删除。
以上是我个人对CMakeList.txt的一个认知。如果有错误或者概念混肴的情况,希望大家积极指出。