ROS cmakelist解读(官方)

ROS中cmakelist与C++中有所不同,稍作记录。

首先是顺序

cmakelist撰写顺序

  1. Required CMake Version (cmake_minimum_required) //cmake版本

  2. Package Name (project()) //包的名字,也是项目名字,并且可以在之后需要使用项目名称时用${PROJECT_NAME} 替代,好处是,改变项目名字时,只需要改变这里的名字,后续的都会改变。

  3. Find other CMake/Catkin packages needed for build (find_package()) //查找依赖包,并且给出找到包的导出路径、库文件等。

  4. Enable Python module support (catkin_python_setup())

  5. Message/Service/Action Generators(add_message_files(), add_service_files(), add_action_files())

  6. Invoke message/service/action generation (generate_messages())

  7. Specify package build info export (catkin_package())

  8. Libraries/Executables to build (add_library()/add_executable()/target_link_libraries())

  9. Tests to build (catkin_add_gtest())

  10. Install rules (install())

必须要按照上面的顺序进行cmakelist的撰写,否则可能会出错。

find_package返回变量名

 _:

  • _FOUND - Set to true if the library is found, otherwise false

  • _INCLUDE_DIRS or _INCLUDES - The include paths exported by the package

  • _LIBRARIES or _LIBS - The libraries exported by the package

  • _DEFINITIONS - 

使用find_package(catkin REQUIRED COMPONENTS pack_name)的好处在于可以让include paths,libraries等加一个catkin_前缀比如catkin_INCLUDE_DIRS并加入到环境变量中,使用起来比较方便。

而如果用原来的,比如说

find_package(catkin REQUIRED COMPONENTS nodelet)

那么就会产生nodelet_INCLUDE_DIRS,nodelet_LIBRARIES等环境变量

catkin_package的相关信息

是一个catkin提供的CMake宏,需要对build系统具体说明catkin的信息用来产生pkg-config和CMake文件。

而且catkin_package()必须写在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 and Library Paths

  • Include Paths - Where can header files be found for the code (most common in C/C++) being built
  • Library Paths - Where are libraries located that executable target build against?
  • include_directories(, ..., )
    在这里使用find_package所产生的环境变量或者其他需要包含的目录,例:
    include_directories(include ${catkin_INCLUDE_DIRS})
    ${catkin_INCLUDE_DIRS}意味着上面所产生的路径环境变量,第一个变量include代表着include/目录下的东西也时路径的一部分。

  • link_directories(, ..., )也是添加路径,但是不推荐。

Executable Targets

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

Library Targets(默认建立共享库)

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

指定要链接的库,一般放到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

 

你可能感兴趣的:(ros)