ROS学习(六):CMakeLists.txt 文件

CMakeLists.txt 文件
为 CMake build 文件。是 CMake 编译系统中软件包的输入。描述如何编译代码、安装到哪里。

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

CMakeLists.txt 格式:

1、Required CMake Version (cmake_minimum_required)
2、Package Name (project())
3、Find other CMake/Catkin packages needed for build (find_package())
4Message/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())
7、Libraries/Executables to build (add_library()/add_executable()/target_link_libraries())
8、Tests to build (catkin_add_gtest())
9Install rules (install())

1、CMake Version:
每一个 catkin CMakeLists.txt 必须以 CMake 需要的版本开始,Catkin 需要版本 2.8.3 或者更高

cmake_minimum_required(VERSION 2.8.3)

2、Package name:
CMake project function 指定的文件名。

project(robot_brain)

在 CMake script 文件中,引用 CMake package 可以使用变量 ${PROJECT_NAME}

3、依赖功能包:

寻找需要用到的其他 CMake packages,用函数 find_package。
至少依赖一个关于 catkin 的功能包

find_package(catkin REQUIRED)

如果要使用 C++ 和 Boost,则需要引用 find_package 包含 Boost,并且指明 Boost 的类型,如使用 Boost threads,则:

find_package(Boost REQUIRED COMPONENTS thread)

4、catkin_package()

catkin_package() 是 catkin 支持的 CMake 宏指令。用来向编译系统指明 catkin-specific 的信息,而编译系统来生成 pkg-config and CMake files。

该函数必须用在用 add_library() or 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
CFG_EXTRAS - Additional configuration options

例子:

catkin_package(
   INCLUDE_DIRS include
   LIBRARIES ${PROJECT_NAME}
   CATKIN_DEPENDS roscpp nodelet
   DEPENDS eigen opencv)

5、Specifying Build Targets

目标文件有两种:
1)、EXE 文件
2)、库文件

目标命名:
注意:必须唯一
重命名用 set_target_properties() 函数,在编译和安装输出中,把目标文件名 rviz_image_view 改为 image_view 如下:

set_target_properties(rviz_image_view
                      PROPERTIES OUTPUT_NAME image_view
                      PREFIX "")

自定义输出路径:

目标文件的默认路径有时候必须改为自定义的路径。自定义的路径中,有一些默认的规则。如包含 Python bindings 的库文件必须将路径设置为 Python 的可 importable 文件夹。

set_target_properties(python_module_library
  PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_PYTHON_DESTINATION})

Include Paths and Library Paths

Include Paths - 头文件路径
Library Paths - 库文件路径
include_directories(, , ..., )
link_directories(, , ..., )

include_directories() 的参数形如 *_INCLUDE_DIRS,变量为 find_package 路径和其他需要包含的路径。例如,使用 catkin and Boost ,形式如下

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

link_directories() 用来加载外加的库函数路径。 catkin and CMake packages 是 find_packaged 时会自动加载链接信息。

link_directories(~/my_libs)

Executable Targets
将 src/main.cpp src/some_file.cpp src/another_file.cpp 编译为文件 myProgram :

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

Library Targets

添加默认共享库

add_library(${PROJECT_NAME} ${${PROJECT_NAME}_SRCS})

target_link_libraries
用在 add_executable() 之后

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

6)、Messages, Services, and Action Targets

处理 messages, services, and actions 的宏有三个:

add_message_files
add_service_files
add_action_files

这些宏的后面必须有调用生成的宏

generate_messages()

这些宏必须在 catkin_package() 之前使用:


find_package(catkin REQUIRED COMPONENTS ...)
 add_message_files(...)
 add_service_files(...)
 add_action_files(...)
 generate_messages(...)
 catkin_package(...)
 ...

必须为 CATKIN_DEPENDS 添加依赖项 message_runtime :


catkin_package(
 ...
 CATKIN_DEPENDS message_runtime ...
 ...)

必须 find_package 文功能包 message_generation,单独或者作为 catkin 的组件。

find_package(catkin REQUIRED COMPONENTS message_generation)

另外,package.xml 需包含 build dependency on message_generation 和 runtime dependency on message_runtime。

如果目标文件包含其他目标文件,而这些目标文件需要 messages/services/actions 进行编译,则需添加
add_dependencies(some_target ${catkin_EXPORTED_TARGETS})
some_target 是用 add_executable() 添加的目标的名字

如果你的功能包编译 messages and/or services as well as executables 时需要用到这些宏,则
add_dependencies(some_target {{PROJECT_NAME}_EXPORTED_TARGETS})
some_target 是用 add_executable() 添加的目标的名字

如果你的功能包同时满足上述条件,则
add_dependencies(some_target $PROJECTNAMEEXPORTEDTARGETS {catkin_EXPORTED_TARGETS})

例子:

msg:”MyMessage1.msg” and “MyMessage2.msg”
depend: std_msgs and sensor_msgs
srv: “MyService.srv”

# Get the information about this package's buildtime dependencies
  find_package(catkin REQUIRED
    COMPONENTS message_generation std_msgs sensor_msgs)

  # Declare the message files to be built
  add_message_files(FILES
    MyMessage1.msg
    MyMessage2.msg
  )

  # Declare the service files to be built
  add_service_files(FILES
    MyService.srv
  )

  # Actually generate the language-specific message and service files
  generate_messages(DEPENDENCIES std_msgs sensor_msgs)

  # Declare that this catkin package's runtime dependencies
  catkin_package(
   CATKIN_DEPENDS message_runtime std_msgs sensor_msgs
  )

  # define executable using MyMessage1 etc.
  add_executable(message_program src/main.cpp)
  add_dependencies(message_program ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

  # define executable not using any messages/services provided by this package
  add_executable(does_not_use_local_messages_program src/main.cpp)
  add_dependencies(does_not_use_local_messages_program ${catkin_EXPORTED_TARGETS})

如果,编译 actionlib actions,需添加 actionlib_msgs 到 find_packaged 的关于 catkin 的组件中,并在 generate_messages(…) 之前添加如下代码:

add_action_files(FILES
  MyAction.action
)

7)、Unit Tests

catkin_add_gtest(myUnitTest test/utest.cpp)

8)、可选项:Installable Targets

CMake install() 函数参数

TARGETS - which targets to install
ARCHIVE DESTINATION - Static libraries and DLL (Windows) .lib stubs
LIBRARY DESTINATION - Non-DLL shared libraries and modules
RUNTIME DESTINATION - Executable targets and DLL (Windows) style shared libraries

例子:

install(TARGETS ${PROJECT_NAME}
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

python包必须被安装到能够被引用的路径

install(TARGETS python_module_library
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_PYTHON_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_PYTHON_DESTINATION}
)

安装 Python Executable Scripts

catkin_install_python(PROGRAMS scripts/myscript
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

安装头文件

install(DIRECTORY include/${PROJECT_NAME}/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
  PATTERN ".svn" EXCLUDE
)
install(DIRECTORY include/
  DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}
  PATTERN ".svn" EXCLUDE
)

安装 roslaunch Files or Other Resources

install(DIRECTORY launch/
  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
  PATTERN ".svn" EXCLUDE)
# install moveit plugin description file
install(FILES robot_moveit_plugins.xml
  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}

你可能感兴趣的:(ROS)