【快速查詢】https://cmake.org/cmake/help/v2.8.8/cmake.html#section_Commands
1 CMake簡介
CMake是跨平台編譯工具,比make更高級一些。其編譯的主要工作是生成CMakeLists.txt文件,然后根據該文件生成Makefile,最后調用make來生成可執行程序或者動態庫。所以基本步驟就只有兩步:(1)cmake生成CMakeLists.txt文件;(2)make執行編譯工作。
下面一張圖對比一下AutoTools與CMake的工作流程(可見CMake比較清晰簡潔):

2、CMakeLists.txt文件
詳情參考:http://wiki.ros.org/catkin/CMakeLists.txt
1、Required CMake Version (cmake_minimum_required) 2、Package Name (project()) 3、Find other CMake/Catkin packages needed for build (find_package()) 4、Message/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()) 9、Install 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
3 一個簡單的CMakeLists.txt入門示例
外部編譯:單獨建立一個空目錄專門用於cmake編譯,便於管理編譯中間文件。在空目錄中用 cmake ../(CMakeLists.txt所在目錄)就行了,中間文件生成在當前目錄。
工程結構:
. ├── build ├── CMakeLists.txt ├── include │ └── math_lib.h └── src ├── main.cpp └── math_lib.cpp 3 directories, 4 files
CMakeLists.txt示例(借鑒網友配置):
# 1.cmake verson,指定cmake版本 cmake_minimum_required(VERSION 3.4.1) # 2.project name,指定項目的名稱,一般和項目的文件夾名稱對應 PROJECT(test_math_lib) # 3.head file path,頭文件目錄 INCLUDE_DIRECTORIES(include) # 4.source directory,源文件目錄 AUX_SOURCE_DIRECTORY(src DIR_SRCS) # 5.set environment variable,設置環境變量,編譯用到的源文件全部都要放到這里,否則編譯能夠通過,但是執行的時候會出現各種問題,比如"symbol lookup error xxxxx , undefined symbol" SET(TEST_MATH ${DIR_SRCS}) # 6.add executable file,添加要編譯的可執行文件 ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_MATH}) # 7.add link library,添加可執行文件所需要的庫,比如我們用到了libm.so(命名規則:lib+name+.so),就添加該庫的名稱 TARGET_LINK_LIBRARIES(${PROJECT_NAME} m)
編譯方法(直接生成目標程序):
kuliuheng@ubuntu:~/_8GB_EXT/workspace/cpp/testCmake$ cd build/ kuliuheng@ubuntu:~/_8GB_EXT/workspace/cpp/testCmake/build$ cmake ../ -- The C compiler identification is GNU 7.4.0 -- The CXX compiler identification is GNU 7.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/kuliuheng/_8GB_EXT/workspace/cpp/testCmake/build
