概括:
一.总的CMakeLists.txt
指定可执行文件存放目录为ch13/bin:
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
指定生成的库存放目录为ch13/lib:
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
找第三方库,并包含它们的头文件
把第三方库文件的库重命名成THIRD_PARTY_LIBS:
set(THIRD_PARTY_LIBS …)
不知道干啥:似乎是告诉工程头文件和cpp文件都在哪里放的
include_directories(头文件路径)
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(app)
二.src的CMakeLists.txt
将众多cpp文件生成共享库:
add_library(myslam SHARED ***1.cpp ***2.cpp …)
给这个共享库链接之前定义的第三方库(${THIRD_PARTY_LIBS}):
target_link_libraries(myslam ${THIRD_PARTY_LIBS})
三.app的CMakeLists.txt
将run_kitti_stereo.cpp生成可执行文件run_kitti_stereo:
add_executable(run_kitti_stereo run_kitti_stereo.cpp)
并将众多cpp文件生成的库(myslam库)与第三方库(${THIRD_PARTY_LIBS})连接到头文件:
target_link_libraries(run_kitti_stereo myslam ${THIRD_PARTY_LIBS})
1.list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
CMake–List用法
如果没有cmake_modules文件夹或者指令写错,报错:
注意CMAKE_MOUDULE_PATH不要打错
CMAKE_MODULE_PATH 定义自己的cmake模块所在的路径
CMake Error at CMakeLists.txt:29 (find_package):
By not providing "FindG2O.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "G2O", but
CMake did not find one.
Could not find a package configuration file provided by "G2O" with any of
the following names:
G2OConfig.cmake
g2o-config.cmake
Add the installation prefix of "G2O" to CMAKE_PREFIX_PATH or set "G2O_DIR"
to a directory containing one of the above files. If "G2O" provides a
separate development package or SDK, be sure it has been installed.
-- Configuring incomplete, errors occurred!
See also "/home/zxj3/code/my_slambook/ch13_0804/build/CMakeFiles/CMakeOutput.log".
2.enable_testing()
最简单的使用ctest的方法,就是在 CMakeLists.txt 添加命令:
enable_testing()
该命令需要在源码的根目录文件内。
从这一刻起,就可以在工程中添加add_test命令了
add_test(NAME <name> [CONFIGURATIONS [Debug|Release|...]]
[WORKING_DIRECTORY dir]
COMMAND <command> [arg1 [arg2 ...]])
name 指定一个名字
Debug|Release 控制那种配置下生效
dir 设置工作目录
command
如果是可执行程序目标,则会被cmake替换成生成的程序的全路径
后面的参数可以使用 <TARGET_FILE:tgt> 指代tgt这个目标的全名
参考:https://www.jianshu.com/p/9cd03de5651a
3.add_subdirectory(src) add_subdirectory(test) add_subdirectory(app)
看来add_subdirectory参数文件必须包含CMakeLists.txt,否则会报错:
CMake Error at CMakeLists.txt:62 (add_subdirectory):
The source directory
/home/zxj3/code/my_slambook/ch13_0804/test
does not contain a CMakeLists.txt file.
-- Configuring incomplete, errors occurred!
See also "/home/zxj3/code/my_slambook/ch13_0804/build/CMakeFiles/CMakeOutput.log".
See also "/home/zxj3/code/my_slambook/ch13_0804/build/CMakeFiles/CMakeError.log".
4.add_library(myslam SHARED ***1.cpp ***2.cpp …)
add_library(libsugan ${SRC_LISTS})
#将集合里的所有的源文件生成一个静态库,该静态库的名字libsugan,注意,在整个CmakeLists里都要用libsugan这个#名字来代替之前那个集合生成的库。
5.set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
EXECUTABLE_OUTPUT_PATH 重新定义目标二进制可执行文件的存放位置
一些宏定义参考:
cmake使用示例与整理总结
6.set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
LIBRARY_OUTPUT_PATH 重新定义目标链接库文件的存放位置
lib下生成立libmyslam.so
7.set(CMAKE_BUILD_TYPE Release)
是Release而不是Realease