1. Package name
project(robot_brain)
项目名需与package名一致。
2. Finding Dependent CMake Packages
find_package(catkin REQUIRED COMPONENTS nodelet)
利用find_package()语法,将此package所依赖的其他package,以components形式find进来,作为catkin的组件。
-
find_package所做的事
如果一个package被find到了,CMake会定义几个环境变量,供引用,命名格式与内容如下
-
<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 - ?
-
-
将其他package以components形式find进来的原因
将package作为catkin组件find进来时,除了定义上面那些常规环境变量,还将定义catkin的一些环境变量,例如
catkin_INCLUDE_DIRS
3. catkin_package()
被用来指定一些catkin特定的信息,拥有五个参数
-
INCLUDE_DIRS
- The exported include paths (i.e. cflags) for the package此参数用来指定外部依赖此package时头文件的路径
-
LIBRARIES
- The exported libraries from the project此参数用来指定外部依赖此package时链接库的路径
-
CATKIN_DEPENDS
- Other catkin projects that this project depends on此package依赖的其他package(属于catkin)
-
DEPENDS
- Non-catkin CMake projects that this project depends on.此package依赖的其他package(不属于catkin,例如我们自己编译安装的opencv就可以用DEPENDS来修饰)
-
CFG_EXTRAS
- Additional configuration options暂时不清楚
以下类似报错常常由catkin_package错误设置或没设置引起
-
fatal error: show/show.hpp: 没有那个文件或目录
被依赖的库的catkin_package没有设置INCLUDE_DIRS,或者设的和引用的不一样,总之检查此参数即可
-
对cv::waitKey(int)未定义的引用
没有链接上被依赖package的动态/静态库,或是没有链接上被间接依赖的package的动态/静态库。
检查对应被依赖package所add_library()生成的库名称,检查依赖package有没有target_lick_libraries()对应库(比如这里就是没有链接opencv库)
一般不用设置LIBRARIES .
4. Specifying Build Targets
-
Include Paths
find_package()后,应有*_INCLUDE_DIRS这个指代头文件目录的环境变量,利用它将所有头文件目录加进去。
include_directories(include ${catkin_INCLUDE_DIRS})
The first argument "include" indicates that the
include/
directory within the package is also part of the path. -
Library Paths
find_package()后,所有catkin和CMake都将自动添加各自的link信息。只需用
target_link_libraries
即可简单的进行添加link_directories(~/my_libs)
也可用ling_directories()添加link的目录(此处有疑问)
-
Executable Targets
对于需要生成的可执行文件,必须使用以下指令来生成
add_executable(myProgram src/main.cpp src/some_file.cpp src/another_file.cpp)
会生成一个名叫
myProgram
的可执行文件 -
Library Targets
使用下面的指令生成链接库文件(catkin生成动态库)
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
有些情况下可以使用${catkin_LIBRARIES} (此处有疑问)
一个注意点:大多数情况下,我们不需要事先执行
link_directories
来为链接库指定路径,因为它一般都会在find_package()时就被定义好了,我们直接target_link_libraries
就好。