参考:http://wiki.ros.org/catkin/CMakeLists.txt
https://answers.ros.org/question/58498/what-is-the-purpose-of-catkin_depends/
1.简介
文件CMakeLists.txt是CMake构建系统的输入,用于构建软件包。 任何兼容CMake的软件包都包含一个或多个CMakeLists.txt文件,这些文件描述了如何构建代码以及将代码安装到何处
Required CMake Version (cmake_minimum_required())
Package Name (project())
Find other CMake/Catkin packages needed for build (find_package())
Enable Python module support (catkin_python_setup())
Message/Service/Action Generators (add_message_files(), add_service_files(), add_action_files())
Invoke message/service/action generation (generate_messages())
Specify package build info export (catkin_package())
Libraries/Executables to build (add_library()/add_executable()/target_link_libraries())
Tests to build (catkin_add_gtest())
Install rules (install())
2.内容
cmake版本至少2.8.3及以上
cmake_minimum_required(VERSION 2.8.3)
项目名称
project(package_name)
在cmake脚本中可以用${PROJECT_NAME}
变量代替项目名
查找依赖的CMake软件包
我们需要使用 find_package
函数来指定需要找到哪些其他CMake软件包来构建我们的项目
find_package(catkin REQUIRED)
find_package(nodelet REQUIRED)
也可以等价写成组件的形式如下:
find_package(catkin REQUIRED COMPONENTS nodelet)
如果CMake通过find_package找到了一个包,它将导致创建多个CMake环境变量,这些变量提供有关找到的包的信息。 环境变量可以稍后在CMake脚本中使用。 环境变量描述了软件包导出的头文件在哪里,源文件在哪里,软件包所依赖的库以及这些库的路径。 名称始终遵循<PACKAGE NAME> _ <PROPERTY>
的约定。
添加消息、服务、动作
结构
find_package(catkin REQUIRED COMPONENTS ...)
add_message_files(...)
add_service_files(...)
add_action_files(...)
generate_messages(...)
catkin_package(...)
...
catkin_package() macro must have a CATKIN_DEPENDS dependency on message_runtime.
catkin_package(
...
CATKIN_DEPENDS message_runtime ...
...)
must use find_package() for the package message_generation, either alone or as a component of catkin:
find_package(catkin REQUIRED COMPONENTS message_generation)
catkin_package() 是catkin提供的CMake宏。 向构建系统指定特定于catkin的信息所必需的,该信息又用于生成pkg-config和CMake文件。
在使用add_library()或add_executable()声明任何目标之前,必须先调用此函数。 该函数具有5个可选参数:
INCLUDE_DIRS
- The exported include paths (i.e. cflags) for the package
LIBRARIES
- The exported libraries from the project
CATKIN_DEPENDS
- Other catkin projects that this project depends on
DEPENDS
- Non-catkin CMake projects that this project depends on. For a better understanding, see this explanation.
CFG_EXTRAS
- Additional configuration options
Include Paths - Where can header files be found for the code (most common in C/C++) being built
include_directories(<dir1>, <dir2>, ..., <dirN>)
link_directories(<dir1>, <dir2>, ..., <dirN>)
include_directories的参数应该是find_package调用生成的* _INCLUDE_DIRS变量以及需要包含的所有其他目录。 如果您使用的是catkin和Boost,则include_directories()调用应如下所示:
include_directories(include ${Boost_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})
第一个参数“ include
”表示包中的include
目录也是路径的一部分。
Library Paths - Where are libraries located that executable target build against?
link_directories()
函数可用于添加其他库路径,但是不建议这样做。 当所有catkin和CMake软件包被find_packaged时,将自动添加其链接信息。 只需链接到target_link_libraries()
中的库。
想用一个第三方库curl如下
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIR})
target_link_libraries(curltest ${CURL_LIBRARY})
Executable Targets 指定可执行文件
add_executable(myProgram src/main.cpp src/some_file.cpp src/another_file.cpp)
这将构建一个名为myProgram的目标可执行文件,该文件由3个源文件构建:src / main.cpp,src / some_file.cpp和src / another_file.cpp。
Library Targets 指定要构建的库
add_library(${PROJECT_NAME} ${${PROJECT_NAME}_SRCS})
target_link_libraries 使用target_link_libraries()
函数来指定可执行目标链接到的库。 通常在add_executable()
调用之后完成此操作。
target_link_libraries(<executableTargetName>, <lib1>, <lib2>, ... <libN>)
例:
add_executable(foo src/foo.cpp)
add_library(moo src/moo.cpp)
target_link_libraries(foo moo)
在大多数情况下,无需使用link_directories()
,因为该信息是通过find_package()
自动提取的。
例:
1 # Get the information about this package's buildtime dependencies
2 find_package(catkin REQUIRED
3 COMPONENTS message_generation std_msgs sensor_msgs)
4
5 # Declare the message files to be built
6 add_message_files(FILES
7 MyMessage1.msg
8 MyMessage2.msg
9 )
10
11 # Declare the service files to be built
12 add_service_files(FILES
13 MyService.srv
14 )
15
16 # Actually generate the language-specific message and service files
17 generate_messages(DEPENDENCIES std_msgs sensor_msgs)
18
19 # Declare that this catkin package's runtime dependencies
20 catkin_package(
21 CATKIN_DEPENDS message_runtime std_msgs sensor_msgs
22 )
23
24 # define executable using MyMessage1 etc.
25 add_executable(message_program src/main.cpp)
26 add_dependencies(message_program ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
27
28 # define executable not using any messages/services provided by this package
29 add_executable(does_not_use_local_messages_program src/main.cpp)
30 add_dependencies(does_not_use_local_messages_program ${catkin_EXPORTED_TARGETS})