CMakeLists.txt基本指令介绍

背景

CMakeLists.txt是编译源包的过程中catkin所依据的文件,里面包含了所需要编译的包的编译器、名称、依赖、路径等等信息。为了便于大家新建一个代码后在ROS编译,现介绍CMakeLists.txt里面的基本指令。

 

指令介绍

cmake_minimum_required(VERSION 2.8.3)

版本声明,需要2.8.3版本或者更高

 

project(chapter8_tutorials)

cmake时创建包的名字,如chapter8_tutorials

 

往往我们需要利用find_package()函数找到一些依赖包来建立我们的新包,至少需要给一个依赖给catkin。

在建立包的过程中给了包环境变量的信息,环境变量提供了:头文件是什么、源文件在哪、库文件的依赖是什么以及库文件的路径是什么。

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  tf
)

给建立新包所需所处不同文件夹的依赖,如roscpp,rospy,tf

加COMPONENTS限定是可以将环境变量的信息直接添加到catkin_variables里面,如果直接

find_package(nodelet)

将不能如此。

在同一个文件夹用下面这句

find_package(catkin REQUIRED)

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES chapter8_tutorials
#  CATKIN_DEPENDS roscpp rospy
#  DEPENDS system_lib
)

设定catkin的信息,必须声明在add_excutable()和add_librariey()前面

 

include_directories(
  ${catkin_INCLUDE_DIRS}
)

包含的目录,由find_package整合使用,含有目录的路径信息。

进一步的,如果使用boost,你的include_directories看起来是这样的:

include_directories(include ${Boost_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})

第一个参数指明了include/ 的目录同样是一个建包所需路径的一部分。

 

add_executable(tf_broadcast src/tf_broadcaster.cpp)
add_executable(tf_listener src/tf_listener.cpp)
add_executable(laser src/laser.cpp)
add_executable(odometry src/odometry.cpp)
add_executable(base_controller src/base_controller.cpp)

添加可执行文件的路径,以tf_broadcaster.cpp文件为例:

第一个参数表示编译后该cpp对应的编译文件名称,一般与.cpp前的文件名同;

第二个参数是指,tf_broadcaster.cpp在CMakeLists.txt所在文件夹同级目录的src文件夹里面(即这个cpp所在的文件夹src与CMakeLists.txt在同一个文件夹),所以可用src/tf_broadcaster.cpp表示。

图层如下图:

这是我们编写代文件后编译前需要的步骤,需要特别注意,python语言后缀是.py,c++的是.cpp。

 

target_link_libraries(tf_broadcast ${catkin_LIBRARIES})
target_link_libraries(tf_listener ${catkin_LIBRARIES})
target_link_libraries(laser ${catkin_LIBRARIES})
target_link_libraries(odometry ${catkin_LIBRARIES})
target_link_libraries(base_controller ${catkin_LIBRARIES})

第一个参数:是指你前面编译后文件名,如tf_broadcast。

第二个参数:添加可执行文件所依赖的库。如tf_broadcast.cpp用到了

#include <ros/ros.h>
#include <tf/transform_broadcaster.h>

这两个头文件ros/ros.h和tf/transform_broadcaster.h已经自动由find_package添加了,一般不需要特别指定,所以用了${catkin_LIBRARIES}。

如果需要特别指定,参照下面这句:

add_executable(foo src/foo.cpp)
add_library(moo src/moo.cpp)
target_link_libraries(foo moo)  -- This links foo against libmoo.so

这也是我们编写代文件后编译前需要的步骤,需要特别注意.

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

参考资料

http://wiki.ros.org/catkin/CMakeLists.txt

 

注释的部分一般都是从模板上没有使用但遗留下来的。

由于实例代码没用用到所有指令和时间原因,所以本次的介绍

未完待续

以后学习过程有机会再补上。

实例代码如下:

cmake_minimum_required(VERSION 2.8.3)
project(chapter8_tutorials)

## 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
  tf
)

## 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 and a run_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependencies might have been
##     pulled in transitively but can be declared for certainty nonetheless:
##     * add a build_depend tag for "message_generation"
##     * 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  # Or other packages containing msgs
# )

###################################
## 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 you 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 chapter8_tutorials
#  CATKIN_DEPENDS roscpp rospy
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
include_directories(
  ${catkin_INCLUDE_DIRS}
)

## Declare a cpp library
# add_library(chapter8_tutorials
#   src/${PROJECT_NAME}/chapter8_tutorials.cpp
# )

## Declare a cpp executable
# add_executable(chapter8_tutorials_node src/chapter8_tutorials_node.cpp)

## Add cmake target dependencies of the executable/library
## as an example, message headers may need to be generated before nodes
# add_dependencies(chapter8_tutorials_node chapter8_tutorials_generate_messages_cpp)

add_executable(tf_broadcast src/tf_broadcaster.cpp)
add_executable(tf_listener src/tf_listener.cpp)
add_executable(laser src/laser.cpp)
add_executable(odometry src/odometry.cpp)
add_executable(base_controller src/base_controller.cpp)



## Specify libraries to link a library or executable target against
# target_link_libraries(chapter8_tutorials_node
#   ${catkin_LIBRARIES}
# )

target_link_libraries(tf_broadcast ${catkin_LIBRARIES})
target_link_libraries(tf_listener ${catkin_LIBRARIES})
target_link_libraries(laser ${catkin_LIBRARIES})
target_link_libraries(odometry ${catkin_LIBRARIES})
target_link_libraries(base_controller ${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 chapter8_tutorials chapter8_tutorials_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_chapter8_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)
posted @ 2019-04-06 20:57  狂小虎  阅读(132)  评论(0编辑  收藏  举报