CMakeList.txt的基本语法规则
CmakeLists.txt书写规范
ROS下的CmakeList基本与之前的CmakeList.txt一致,仅仅添加了一些ROS相关的内容
#最顶端
cmake_minimum_required(VERSION 2.8.3)
project(gscam) //表示当前软件包的名称
他们的语法规则
[version] [EXACT] [QUIET] [NO_MODULE] [ [ REQUIRED | COMPONENTS ] [ componets… ] ] )name 和version是库的名称和版本
后面的参数
EXACT 要求版本号必须精确匹配
QUIET 没有找到也不会有警告信息,会禁掉包没有被发现时的警告信息
REQUIRED 如果报没有找到的话,cmake的过程会终止,并输出警告信息(说明这个module是工程必须有的)
COMPONENTS COMPONENTS选项之后,可以列出一些与包相关(依赖)的部件清单(components list)
例如
find_package(PkgConfig) #查找PkgConfig所指向库文件和头文件find_package(catkin REQUIRED
COMPONENTS roscpp image_transport sensor_msgs nodelet
camera_calibration_parsers camera_info_manager
)
[QUIET] [REQUIRED]
)检测所有给出的modules
PREFIX 自定义的前缀,查找到之后,可以用这个前缀来代表相应的module
QUIET/ REQUIRED 和前面的类似
QUIET/ REQUIRED的区别,
如果没找到,一个不会报错,接着继续进行,一个会警告,并终止Cmake
如果找的话,无论是哪一种模式,都可以使用下面这些变量
<NAME>_FOUND
<NAME>_INCLUDE_DIRS or <NAME>_INCLUDES //头文件
<NAME>_LIBRARIES or <NAME>_LIBRARIES or <NAME>_LIBS //库文件
<NAME>_DEFINITIONS
看一个比较典型的例子
实现的功能就是在ROS工程中,使用gstreamer库
首先考虑gstreamer-1.0,其次考虑gstreamer-0.10,如果都没有,Cmake就进行不下去,
无论有哪一个下面的
后面的
catkin_package 可以DEPENDS GSTREAMER GST_APP
include_directories 可以包含 ${GST_APP_INCLUDE_DIRS}
add_library 可以包含 ${GSTREAMER_LIBRARIES}
${GST_APP_LIBRARIES}
# System Dependencies
find_package(PkgConfig) #查找PkgConfig所指向库文件和头文件
#通过PkgConfig的环境变量下配置的 ××.pc 管理了很多软件的库文件和头文件
pkg_check_modules(GSTREAMER QUIET gstreamer-0.10)
# 查找gstreamer-0.10模组 ,并且自定义前缀GSTREAMER
if(NOT GSTREAMER_FOUND)
#如果找的了,当然可以使用GSTREAMER_FOUND,甚至后面的_INCLUDE_DIRS, 没找到当然就是 NOT GSTREAMER_FOUND
set(GSTREAMER_VERSION_1_x TRUE) #设置一个变量GSTREAMER_VERSION_1_x = 1
endif()
if(GSTREAMER_VERSION_1_x) #GSTREAMER_VERSION_1_x为真,说明没有gstreamer-0.10
message(STATUS "gst 1.0") #暂且先把消息类型为STATUS类型的消息设置为 "gst 1.0"
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0)
pkg_check_modules(GST_APP REQUIRED gstreamer-app-1.0)
#再查找一下gstreamer-1.0,gstreamer-app-1.0 这回查找的条件是REQUIRED的
else() #GSTREAMER_VERSION_1_x为假,说明有gstreamer-0.10
message(STATUS "gst 0.1")
pkg_check_modules(GSTREAMER REQUIRED gstreamer-0.10)
pkg_check_modules(GST_APP REQUIRED gstreamer-app-0.10)
#再查找一下gstreamer-0.10,gstreamer-app-0.10 这回查找的条件是REQUIRED的
endif()
The find_package command is common cmake and is needed to load the catkin macros and specify dependencies to other ROS packages.
find_package(catkin REQUIRED
COMPONENTS roscpp image_transport sensor_msgs nodelet
camera_calibration_parsers camera_info_manager
)
###################################
## 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 gstcamera
# CATKIN_DEPENDS other_catkin_pkg
# DEPENDS system_lib
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES gscam
CATKIN_DEPENDS roscpp nodelet image_transport sensor_msgs
camera_calibration_parsers camera_info_manager
DEPENDS GSTREAMER GST_APP
)
catkin_package的一些参数
macro defined in catkin_package.cmake
Parameters
INCLUDE_DIRS 当前工程下的include
– CMAKE_CURRENT_SOURCE_DIR-relative paths to C/C++ includesLIBRARIES 和find_package同样的 ros内部依赖的库
– names of library targets that will appear in the catkin_LIBRARIES and ${PROJECT_NAME}_LIBRARIES of other projects that search for you via find_package. #CATKIN_DEPENDS – a list of catkin projects which this project depends on.DEPENDS 工程依赖的外部库
– a list of CMake projects which this project depends on.
include_directories(
include #当前工程下的include
${catkin_INCLUDE_DIRS} #
${GLIB_INCLUDE_DIRS}
${GST_APP_INCLUDE_DIRS})
add a library to the project using the specified source files.
Link a target to given libraries. 一般在add_library,add_executable之后
add_library(gscam src/gscam.cpp)
target_link_libraries(gscam
${catkin_LIBRARIES}
${GSTREAMER_LIBRARIES}
${GST_APP_LIBRARIES})
add_executable(gscam_node src/gscam_node.cpp)
target_link_libraries(gscam_node gscam
${catkin_LIBRARIES}
${GSTREAMER_LIBRARIES}
${GST_APP_LIBRARIES})
set_target_properties(gscam_node PROPERTIES OUTPUT_NAME gscam)
#变更target的属性 这里是改它的名字