Ros学习——Cmakelists.txt文件解读

0.目的

cmakelist:用于定义package的编译过程

1.过程

 

1、Required CMake Version (cmake_minimum_required)  //CMake 需要的版本
2、Package Name (project())    //#定义工程名称
3、Find other CMake/Catkin packages needed for build (find_package())        //寻找需要用到的其他 CMake packages

4、Message/Service/Action Generators (add_message_files(), add_service_files(), add_action_files())
5、Invoke message/service/action generation (generate_messages())
6、Specify package build info export (catkin_package())//catkin 支持的 CMake 宏指令。用来向编译系统指明 catkin-specific 的信息,而编译系统来生成 pkg-config and CMake files。
7、Libraries/Executables to build (add_library()/add_executable()/target_link_libraries())
8、Tests to build (catkin_add_gtest())
9、Install rules (install())

 

2.示例

amcl_node.cpp的cmakelists.txt

cmake_minimum_required(VERSION 3.1)//Catkin 需要版本 3.1或者更高
project(amcl)    //定义工程名称

if(NOT CMAKE_CXX_STANDARD)    //设置编译环境
  set(CMAKE_CXX_STANDARD 11)
endif()

find_package(catkin REQUIRED//寻找需要用到的其他 CMake packages
  COMPONENTS
    dynamic_reconfigure
    geometry_msgs
    message_filters
    nav_msgs
    rosbag
    roscpp
    sensor_msgs
    std_srvs
    tf2
    tf2_geometry_msgs
    tf2_msgs
    tf2_ros
)

find_package(Boost REQUIRED)

# dynamic reconfigure
generate_dynamic_reconfigure_options(
    cfg/AMCL.cfg
)

catkin_package(//向编译系统指明 catkin-specific 的信息
  CATKIN_DEPENDS
    dynamic_reconfigure
    geometry_msgs
    nav_msgs
    rosbag
    roscpp
    sensor_msgs
    std_srvs
    tf2
    tf2_msgs
    tf2_ros
  INCLUDE_DIRS include
  LIBRARIES amcl_sensors amcl_map amcl_pf
)

include_directories(include)//变量为 find_package 路径和其他需要包含的路径
include_directories(${catkin_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})

add_library(amcl_pf//添加默认共享库
                    src/amcl/pf/pf.c
                    src/amcl/pf/pf_kdtree.c
                    src/amcl/pf/pf_pdf.c
                    src/amcl/pf/pf_vector.c
                    src/amcl/pf/eig3.c
                    src/amcl/pf/pf_draw.c)

add_library(amcl_map
                    src/amcl/map/map.c
                    src/amcl/map/map_cspace.cpp
                    src/amcl/map/map_range.c
                    src/amcl/map/map_store.c
                    src/amcl/map/map_draw.c)

add_library(amcl_sensors
                    src/amcl/sensors/amcl_sensor.cpp
                    src/amcl/sensors/amcl_odom.cpp
                    src/amcl/sensors/amcl_laser.cpp)
target_link_libraries(amcl_sensors amcl_map amcl_pf)//表示前面的库依赖后面的库


add_executable(amcl
                       src/amcl_node.cpp)
add_dependencies(amcl ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

target_link_libraries(amcl
    amcl_sensors amcl_map amcl_pf
    ${Boost_LIBRARIES}
    ${catkin_LIBRARIES}
)

install( TARGETS//生成Makefile中的install target,把这些对应文件拷贝到目标目录下
    amcl amcl_sensors amcl_map amcl_pf
    ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

install(DIRECTORY include/amcl/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)

install(DIRECTORY examples/
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/examples
)

## Configure Tests
if(CATKIN_ENABLE_TESTING)
  find_package(rostest REQUIRED)

  # Bags
  catkin_download_test_data(${PROJECT_NAME}_basic_localization_stage_indexed.bag
    http://download.ros.org/data/amcl/basic_localization_stage_indexed.bag
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 41fe43af189ec71e5e48eb9ed661a655)
  catkin_download_test_data(${PROJECT_NAME}_global_localization_stage_indexed.bag
    http://download.ros.org/data/amcl/global_localization_stage_indexed.bag
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 752f711cf4f6e8d1d660675e2da096b0)
  
//下载文件
catkin_download_test_data(${PROJECT_NAME}_small_loop_prf_indexed.bag
    http://download.ros.org/data/amcl/small_loop_prf_indexed.bag
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 e4ef0fc006872b43f12ed8a7ce7dcd81)
  catkin_download_test_data(${PROJECT_NAME}_small_loop_crazy_driving_prg_indexed.bag
    http://download.ros.org/data/amcl/small_loop_crazy_driving_prg_indexed.bag
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 4a58d1a7962914009d99000d06e5939c)
  catkin_download_test_data(${PROJECT_NAME}_texas_greenroom_loop_indexed.bag
    http://download.ros.org/data/amcl/texas_greenroom_loop_indexed.bag
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 6e3432115cccdca1247f6c807038e13d)
  catkin_download_test_data(${PROJECT_NAME}_texas_willow_hallway_loop_indexed.bag
    http://download.ros.org/data/amcl/texas_willow_hallway_loop_indexed.bag
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 27deb742fdcd3af44cf446f39f2688a8)
  catkin_download_test_data(${PROJECT_NAME}_rosie_localization_stage.bag
    http://download.ros.org/data/amcl/rosie_localization_stage.bag
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 3347bf3835724cfa45e958c5c1846066)

  # Maps
  catkin_download_test_data(${PROJECT_NAME}_willow-full.pgm
    http://download.ros.org/data/amcl/willow-full.pgm
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 b84465cdbbfe3e2fb9eb4579e0bcaf0e)
  catkin_download_test_data(${PROJECT_NAME}_willow-full-0.05.pgm
    http://download.ros.org/data/amcl/willow-full-0.05.pgm
    DESTINATION ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_SHARE_DESTINATION}/test
    MD5 b61694296e08965096c5e78611fd9765)

  # Tests
  add_rostest(test/set_initial_pose.xml)
  add_rostest(test/set_initial_pose_delayed.xml)
  add_rostest(test/basic_localization_stage.xml)
  add_rostest(test/small_loop_prf.xml)
  add_rostest(test/small_loop_crazy_driving_prg.xml)
  add_rostest(test/texas_greenroom_loop.xml)
  add_rostest(test/rosie_multilaser.xml)
  add_rostest(test/texas_willow_hallway_loop.xml)

# Not sure when or if this actually passed.
#
# The point of this is that you start with an even probability
# distribution over the whole map and the robot localizes itself after
# some number of iterations of sensing and motion.
#
#  add_rostest(test/global_localization_stage.xml)
endif()
    

 

3.常用语句及语法

3.1 常用指令

cmake_minimum_required(VERSION xxx) #cmake最小版本需求,新版本的cmake改了很多东西,提升了便利性,也可能让你自己挖坑了
project(xxx) #设置此项目的名称
add_executable(target target_source_codes) #生成可执行文件target ,后面填写的是生成此可执行文件所依赖的源文件列表。
SET(var_name var_value)# 设置一个名字var_name 的变量,同时给此变量赋值为var_value
MESSAGE("MSG") #类比echo 打印消息
option(var_name "comment" var_value) #给变量var_name赋值为var_value,comment是此变量的注释,和SET 有类似的功效,用于给某变量设置默认值
include_directories(xxx) #添加include路径,也就是 gcc -I xxx 的意思,或者vs ide中添加头文件包含目录
add_subdirectory(xxx) #调用xxx子目录的CMakeLists.txt执行
add_compile_options(xxx) #给编译器添加xxx参数,但是貌似没有什么用,我一般不这样添加参数,不直接
link_directories(xxx) #给编译器添加库目录,也就是 gcc -L xxx 的意思,或者vs ide中添加库的包含目录
add_library(lib_name SHARED or STATIC lib_source_code) #和add_executable类似,生成库文件,SHARED代表动态库,STATIC代表静态库, 最后一个参数代表此库的源文件列表,此指令只有三个参数
target_link_libraries(target_name lib_name ...) #给目标添加依赖库,类似与gcc -l lib_name,此指令有两个用处,一个是给可执行target_name 添加库依赖,二是给库target_name 添加库依赖。

3.2 install 指令(主要是生成Makefile中的install target)

install(FILES flie DESTINATION dir_path) #执行make install时,把file拷贝到dir_path
install(PROGRAMS file DESTINATION dir_path) #执行make install时,把file拷贝到dir_path,并给予file可执行权限
INSTALL(TARGETS  ylib ylib_s
    #RUNTIME DESTINATION xxx
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
)# 安装libylib.so到lib目录,安装libylib_s.a到lib目录,RUNTIME 是安装可执行文件到xxx目录

注意:

INSTALL(TARGETS  ylib ylib_s
    #RUNTIME DESTINATION xxx
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
)
#对于RUNTIME  和 LIBRARY 两种目标,在安装时候,cmake会默认给你移除掉目标文件中的gcc的Wl,rpath的值,导致某些库找不到的错误。
以下变量会影响此坑,更详细的信息去查查别的资料,我这里就不详细说明了。
#set(CMAKE_SKIP_BUILD_RPATH FALSE)                
#set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)        
#set(CMAKE_INSTALL_RPATH "")                      
#set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)    
#set(CMAKE_SKIP_INSTALL_RPATH TRUE)
#set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

#set(CMAKE_SKIP_RPATH TRUE)
#set(CMAKE_SKIP_INSTALL_RPATH TRUE)

 

 

3.3 config指令

configure_file(fileA fileB @ONLY)
#把fileA 复制并重命名为fileB,此时,fileA中的@var@的值会被替换为cmakelists.txt 中var的值。@ONLY是只转换@va@这种变量

3.4 常用的内置变量

CMAKE_INSTALL_PREFIX  #make install 的安装路径
CMAKE_BUILD_TYPE #生成的目标为debug或者release
CMAKE_C_FLAGS #gcc 的编译参数指定,这个非常好用,一般通过set 修改其值
CMAKE_CXX_FLAGS #g++ 和上面CMAKE_C_FLAGS 类似
CMAKE_CURRENT_SOURCE_DIR # 当前CMakeLists.txt所在的目录,主要用来定位某文件
CMAKE_CURRENT_BINARY_DIR # 当前CMakeLists.txt对应的编译时的目录

 

 

第三部分参考:

作者:Iflyinsky2013
来源:CSDN
原文:https://blog.csdn.net/u011728480/article/details/81480668

posted @ 2019-01-22 10:00  寒江小筑  阅读(1076)  评论(1编辑  收藏  举报