Fork me on GitHub

ros之CMakeLists文件

Overall Structure and Ordering

  • 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())

CMake Version

cmake_minimum_required(VERSION 2.8.3)

Package name

project(robot_brain)

注:在CMakeLists.txt里面可用使用${PROJECT_NAME}来引用工程名。

find_package

find_package(catkin REQUIRED COMPONENTS nodelet) 

注:如果工程依赖于其他功能包,那么作为catkin的component, 这样易于管理使用。

  • 如果找到了功能包,那么会提供包的头文件、库文件等路径作为环境变量使用
<NAME>_FOUND - Set to true if the library is found, otherwise false
<NAME>_INCLUDE_DIRS or <NAME>_INCLUDES - The include paths exported by the package
<NAME>_LIBRARIES or <NAME>_LIBS - The libraries exported by the package
<NAME>_DEFINITIONS - ?

catkin_package()

  • catkin_package()是cmake提供的CMake macro, 用于生成输出, 该函数必须在 add_library() 或add_executable()之前被调用。

The function has 5 optional arguments:

  • 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

Full macro documentation can be found here.

As an example:

#+BEGIN_SRC cmake
catkin_package(
 I# 参考

- [catkin/CMakeLists.txt](http://wiki.ros.org/catkin/CMakeLists.txt)# 参考

- [catkin/CMakeLists.txt](http://wiki.ros.org/catkin/CMakeLists.txt)NCLUDE_DIRS include
 LIBRARIES ${PROJECT_NAME}
 CATKIN_DEPENDS roscpp nodelet
 DEPENDS eigen opencv)
#+# 参考

- [catkin/CMakeLists.txt](http://wiki.ros.org/catkin/CMakeLists.txt)END_SRC cmake

This indicates that the folder "include" within the package folder is where exported headers go.The CMake environment variable ${PROJECT_NAME} evaluates to whatever you passed to the project() function earlier, in this case it will be "robot_brain"."roscpp" + "nodelet" are packages that need to be present to build/run this package, and "eigen" + "opencv" are system dependencies that need to be present to build/run this package.

include_directories()

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

Executable Targets

# 参考

- [catkin/CMakeLists.txt](http://wiki.ros.org/catkin/CMakeLists.txt)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

target_link_libraries(<executableTargetName>, <lib1>, <lib2>, ... <libN>)

Messages, Services, and Action Targets

When compile your projects, it cannot find some msg, srv type, but compile two or more, it compile successful.

find_package(catkin REQUIRED COMPONENTS ...)
add_message_files(...)
add_service_files(...)
add_action_files(...)
generate_messages(...)
catkin_package(...)
...
  • If you have a target which (even transitively) depends on some other target that needs messages/services/actions to be built, you need to add an explicit dependency on target catkin_EXPORTED_TARGETS, so that they are built in the correct order. This case applies almost always, unless your package really doesn't use any part of ROS. Unfortunately, this dependency cannot be automatically propagated. (some_target is the name of the target set by add_executable()):
add_dependencies(some_target ${catkin_EXPORTED_TARGETS})
  • If you have a package which builds messages and/or services as well as executables that use these, you need to create an explicit dependency on the automatically-generated message target so that they are built in the correct order. (some_target is the name of the target set by add_executable()):参考
add_dependencies(some_target ${${PROJECT_NAME}_EXPORTED_TARGETS})
  • If you your package satisfies both of the above conditions, you need to add both dependencies, i.e.:
add_dependencies(some_target ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})  

参考

posted @ 2021-06-26 11:32  chrislzy  阅读(157)  评论(0编辑  收藏  举报