CMakeList.txt语法详解(DeepL密钥邀请)

CMakeList.txt语法详解(DeepL密钥邀请)

这篇文章是关于ROS里使用catkin cmaklist编译工程的语法解释,小白第一次写,仅作为自己学习过程的记录。

这里同时挂一下我的DeepLAPI的邀请链接,欢迎大家使用!!!,zotero里翻译使用DeepL翻译比国内的一些准很多。
邀请链接:https://deepl-pro.com/#/translate?referral_code=pQmXYxoSZM
邀请密钥:pQmXYxoSZM
在这里插入图片描述

  • 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的编译方式做了一个封装的指令工具,规范了工作路径与生成文件路径。

常用命令

1.指定CMake的最小版本
cmake_minimum_required(VERSION 2.8.3)

每一个catkin CMakeList.txt文件必须以所需的CMake版本说明语句开始,Catkin需要2.8.3或者更高的版本

2.功能包包名
project(name)

CMake中,可以通过使用变量 (${PROJECT_NAME} 在CMake脚本后面的任何位置引用项目名称。

3.设置编译类型
add_executable(name name.cpp) # 生成可执行文件
add_library(common STATIC util.cpp) # 生成静态库
add_library(common SHARED util.cpp) # 生成动态库或共享库
4.指定编译包含的源文件
4.1 明确指定包含哪些源文件
add_library(demo demo.cpp test.cpp util.cpp)
4.2 搜索所有的cpp文件
aux_source_directory(. SRC_LIST) # 搜索当前目录下的所有.cpp文件
add_library(demo ${SRC_LIST})

aux_source_directory(dir VAR) 发现一个目录下所有的源代码文件并将列表存储在一个变量中。

5.查找编译所依赖的CMake包
find_package(catkin REQUIRED)

编译一个项目,需要使用CMake的find_package函数确定依赖的其他CMake包并找到他们,一般情况下至少会有一个catkin依赖。

find_package(catkin REQUIRED COMPONENTS 
			roscpp)

除此之外,项目依赖的其他软件包,都会自动成为catkin的组件(COMPONENTS)。因此可以将这些依赖包指定为catkin的组建,而不必再使用==find_package,这样将会变得简洁,例如常用的roscpp等依赖包。

如果CMake通过find_package查找到一个软件包,它就会创建几个CMake环境变量,已提供已查到的软件包的信息,这些环境变量可以在后面的CMake脚本中使用,环境变量的名字遵循< PACKAGENAME >_< PROPERTY >,即包名_属性:

    <NAME>_FOUND:当库被查找到时置为true,否则为false
    <NAME>_INCLUDE_DIRS或<NAME>_INCLUDES:软件包导出的头文件路径
    <NAME>_LIBRARIES或<NAME>_LIBS:软件包导出的库的路径
6.catkin_package()

该函数必须在使用add_library()add_executable() 声明任何targets之前调用,其5个可选参数:

catkin_package(
      INCLUDE_DIRS include                 #软件包导出的头文件路径
      LIBRARIES ${PROJECT_NAME}            #项目导出的库
      CATKIN_DEPENDS nav_mags roscpp ...   #当前项目依赖的其他catkin项目
      DEPENDS eigen opencv ..              #当前项目依赖的非catkin CMake项目
      CFG_EXTRAS                           #其它的配置选项
)
7.include_directories

include_directories的参数应该是调用find_package生成的 *_INCLUDE_DIRS 变量以及需要包含的任何其他目录。

include_directories(
    include                  #表示include/目录也是路径的一部分
    ${catkin_INCLUDE_DIRS}
    ${Boost_INCLUDE_DIRS}
    ${casadi_INCLUDE_DIRS}
    ${Eigen3_INCLUDE_DIRS}
)
8.可执行目标

要指定必须编译的可执行目标,必须使用CMake函数add_executable()。声明想要的可执行文件的文件名,以此生成可执行文件所需的源文件列表,如果有多个源文件,用空格区分开。

add_executable(myprogram src/main.cpp src/some_file.cpp src/another_file.cpp)

该命令会编译名为myprogram的可执行文件,他是由后面的源文件共同编译生成的。

9.target_link_libraries

使用target_link_libraries函数指定可执行目标所要链接的库,即告诉CMake当链接此可执行文件时需要链接哪些库 (这些库在上面的find_package中定义),通常在调用完add_executable后被调用。

target_link_libraries(<executableTargetName>, <lib1>, <lib2>, ... <libN>)
10.消息、服务和操作目标

在被ROS软件包编译和使用之前,ROS中的消息msg,服务srv,动作action文件需要特殊的预处理器编译步骤。这些宏的要点是生成编译语言特定的文件,以便可以在变成语言中使用消息、服务和操作。
对应分别提供了三个宏来分别处理消息、服务和操作:

add_message_files(demo1.msg, demo2.msg...)
add_service_files(demo1.srv, demo2.msg...)
add_action_files(demo1.action, demo2.action...)
  • catkin_package() 宏必须包含一个在 message_runtime上的 CATKIN_DEPENDS 依赖。
CATKIN_DEPENDS message_runtime ... 
  • 必须对软件包message_generation使用find_package(),可单独或者作为catkin的组件使用。
find_package(catkin REQUIRED COMPONENTS message_generation)
  • package.xml 文件必须包含一个在message_generation上的编译依赖和一个在message_runtime上的运行时依赖,如果从其他包中传递依赖关系项,则这个不是必须的。
<build_depend>message_generation</build_depend>            #编译依赖
<run_depend>message_runtime</run_depend>                   #运行时依赖
  • 如果有一个目标(甚至是过渡性的)依赖于需要建立消息/服务/动作的其他目标,需要在目标catkin_EXPORTED_TARGETS上添加显式依赖性,以使它们按照正确的顺序编译,这种情况几乎总是适用。
add_dependencies(some_target ${catkin_EXPORTED_TARGETS})
11.单元测试

特定的catkin宏catkin_add_gtest() 用于处理基于gtest的单元测试:

catkin_add_gtest(myUnitTest test/utest.cpp)

CMakeList.txt文件书写模板

cmake_minimum_required(VERSION 2.8.3)
project(my_p)
 
## 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
  roscpp
  rospy
  std_msgs
)
 
## 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 run_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 run_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 run_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 ##
###################################
## 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 my_p
#  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}/my_p.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/my_p_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_my_p.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

在此感谢以下博主总结:
1.ROS中CMakeLists.txt文件详解
2.CMake——CMakeLists.txt 的详解
3.CMakeLists.txt 语法介绍与实例演练
4.cmake的add_custom_command和add_custom_target指令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值