Cmake全称Cross Platform Make
什么是Cmake
你或许听过好几种 Make 工具,例如 GNU Make ,QT 的 qmake ,微软的 MS nmake,BSD Make(pmake),Makepp,等等。这些 Make 工具遵循着不同的规范和标准,所执行的 Makefile 格式也千差万别。这样就带来了一个严峻的问题:如果软件想跨平台,必须要保证能够在不同平台编译。而如果使用上面的 Make 工具,就得为每一种标准写一次 Makefile ,这将是一件让人抓狂的工作。
CMake 就是针对上面问题所设计的工具:它首先允许开发者编写一种平台无关的 CMakeList.txt 文件来定制整个编译流程,然后再根据目标用户的平台进一步生成所需的本地化 Makefile 和工程文件,如 Unix 的 Makefile 或 Windows 的 Visual Studio 工程。从而做到“Write once, run everywhere”。显然,CMake 是一个比上述几种 make 更高级的编译配置工具。一些使用 CMake 作为项目架构系统的知名开源项目有 VTK、ITK、KDE、OpenCV、OSG 等 。
单目录单文件
main.cpp
该程序的用途是计算一个数的指数幂。
#include <stdio.h> #include <stdlib.h> /** * power - Calculate the power of number. * @param base: Base value. * @param exponent: Exponent value. * * @return base raised to the power exponent. */ double power(double base, int exponent) { int result = base; int i; if (exponent == 0) { return 1; } for(i = 1; i < exponent; ++i){ result = result * base; } return result; } int main(int argc, char *argv[]) { if (argc < 3){ printf("Usage: %s base exponent \n", argv[0]); return 1; } double base = atof(argv[1]); int exponent = atoi(argv[2]); double result = power(base, exponent); printf("%g ^ %d is %g\n", base, exponent, result); return 0; }
编写 CMakeLists.txt 文件
保存在与 main.cpp 源文件同个目录下:
# CMake 最低版本号要求 cmake_minimum_required (VERSION 2.8) # 项目信息 project (demo1) # 指定生成目标 add_executable(demo1 main.cpp)
CMakeLists.txt 命令是不区分大小写的。符号
#
后面的内容被认为是注释。对于上面的 CMakeLists.txt 文件,依次出现了几个命令:
cmake_minimum_required
:指定运行此配置文件所需的 CMake 的最低版本;project
:参数值是demo1
,该命令表示项目的名称是demo1
。add_executable
: 将名为 main.cpp的源文件编译成一个名称为 demo1 的可执行文件。第二个参数是依赖,就是说编译要依赖main.cpp这个文件
编译项目
在当前目录执行
cmake .
得到 Makefile 后再使用 make 命令编译得到 demo1 可执行文件
单目录多文件
上面的例子只有单个源文件。现在假如把
power
函数单独写进一个名为MathFunctions.cpp
的源文件里,使得这个工程变成如下的形式:main.cpp
#include <stdio.h> #include <stdlib.h> #include "MathFunctions.h" int main(int argc, char *argv[]) { if (argc < 3){ printf("Usage: %s base exponent \n", argv[0]); return 1; } double base = atof(argv[1]); int exponent = atoi(argv[2]); double result = power(base, exponent); printf("%g ^ %d is %g\n", base, exponent, result); return 0; }
MathFunctions.cpp
/** * power - Calculate the power of number. * @param base: Base value. * @param exponent: Exponent value. * * @return base raised to the power exponent. */ double power(double base, int exponent) { int result = base; int i; if (exponent == 0) { return 1; } for(i = 1; i < exponent; ++i){ result = result * base; } return result; }
MathFunctions.h
#ifndef POWER_H #define POWER_H extern double power(double base, int exponent); #endif
修改CMakeLists.txt
# CMake 最低版本号要求 cmake_minimum_required (VERSION 2.8) # 项目信息 project (demo2) # 查找目录下的所有源文件 # 并将名称保存到 DIR_SRCS 变量 aux_source_directory(. DIR_SRCS) # 指定生成目标 add_executable(Demo ${DIR_SRCS})
多目录多文件
现在进一步将 MathFunctions.h 和 MathFunctions.cpp 文件移动到 math 目录下。
原代码不需要改,但是注意main.cpp的头文件引用要改一下
对于这种情况,需要分别在项目根目录 Demo3 和 math 目录里各编写一个 CMakeLists.txt 文件。为了方便,我们可以先将 math 目录里的文件编译成静态库再由 main 函数调用。
根目录中的 CMakeLists.txt
# CMake 最低版本号要求 cmake_minimum_required (VERSION 2.8) # 项目信息 project (demo3) # 查找当前目录下的所有源文件 # 并将名称保存到 DIR_SRCS 变量 aux_source_directory(. DIR_SRCS) # 添加 math 子目录 add_subdirectory(math) # 指定生成目标 add_executable(Demo main.cpp) # 添加链接库 target_link_libraries(Demo MathFunctions)
该文件添加了下面的内容: 第3行,使用命令
add_subdirectory
指明本项目包含一个子目录 math,这样 math 目录下的 CMakeLists.txt 文件和源代码也会被处理 。第6行,使用命令target_link_libraries
指明可执行文件 main 需要连接一个名为 MathFunctions 的链接库子目录中的 CMakeLists.txt
# 查找当前目录下的所有源文件 # 并将名称保存到 DIR_LIB_SRCS 变量 aux_source_directory(. DIR_LIB_SRCS) # 生成链接库 add_library (MathFunctions ${DIR_LIB_SRCS}) #这种写法就是静态库
在该文件中使用命令
add_library
将 src 目录中的源文件编译为静态链接库。
自定义编译选项
CMake 允许为项目增加编译选项,从而可以根据用户的环境和需求选择最合适的编译方案。
例如,可以将 MathFunctions 库设为一个可选的库,如果该选项为
ON
,就使用该库定义的数学函数来进行运算。否则就调用标准库中的数学函数库。
没修改的部分都和之前的一样demo3一样,包括math中的文件和math中的CMakeLists.txt
修改 CMakeLists 文件
在顶层的 CMakeLists.txt 文件中添加该选项
# CMake 最低版本号要求 cmake_minimum_required (VERSION 2.8) # 项目信息 project (demo4) # 加入一个配置头文件,用于处理 CMake 对源码的设置 configure_file ( "${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h" ) # 是否使用自己的 MathFunctions 库 option (USE_MYMATH "Use provided math implementation" ON) # 是否加入 MathFunctions 库 if (USE_MYMATH) include_directories ("${PROJECT_SOURCE_DIR}/math") add_subdirectory (math) set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions) endif (USE_MYMATH) # 查找当前目录下的所有源文件 # 并将名称保存到 DIR_SRCS 变量 aux_source_directory(. DIR_SRCS) # 指定生成目标 add_executable(Demo ${DIR_SRCS}) target_link_libraries (Demo ${EXTRA_LIBS})
其中:
第7行的 configure_file 命令用于加入一个配置头文件 config.h ,这个文件由 CMake 从 config.h.in 生成,通过这样的机制,将可以通过预定义一些参数和变量来控制代码的生成。
第13行的 option 命令添加了一个 USE_MYMATH 选项,并且默认值为 ON 。
第17行根据 USE_MYMATH 变量的值来决定是否使用我们自己编写的 MathFunctions 库。
修改main.cpp文件,让其根据 USE_MYMATH 的预定义值来决定是否调用标准库还是 MathFunctions 库
#include <stdio.h> #include <stdlib.h> #include "config.h" #ifdef USE_MYMATH #include "math/MathFunctions.h" #else #include <math.h> #endif int main(int argc, char *argv[]) { if (argc < 3){ printf("Usage: %s base exponent \n", argv[0]); return 1; } double base = atof(argv[1]); int exponent = atoi(argv[2]); #ifdef USE_MYMATH printf("Now we use our own Math library. \n"); double result = power(base, exponent); #else printf("Now we use the standard library. \n"); double result = pow(base, exponent); #endif printf("%g ^ %d is %g\n", base, exponent, result); return 0; }
编写 config.h.in 文件
上面的程序值得注意的是第3行,这里引用了一个 config.h 文件,这个文件预定义了 USE_MYMATH 的值。但我们并不直接编写这个文件,为了方便从 CMakeLists.txt 中导入配置,我们编写一个 config.h.in 文件
config.h.in
#cmakedefine USE_MYMATH
这样 CMake 会自动根据 CMakeLists 配置文件中的设置自动生成 config.h 文件
编译项目
现在编译一下这个项目,为了便于交互式的选择该变量的值,可以使用
ccmake
命令从中可以找到刚刚定义的 USE_MYMATH 选项,按键盘的方向键可以在不同的选项窗口间跳转,按下 enter 键可以修改该选项。修改完成后可以按下 c 选项完成配置,之后再按 g 键确认生成 Makefile 。ccmake 的其他操作可以参考窗口下方给出的指令提示。
我们可以试试分别将 USE_MYMATH 设为 ON 和 OFF 得到的结果:
USE_MYMATH OFF的时候用的就是数学库中的pow函数
cmake .
make
现在输入ccmake .
改为on,再 make
Cmake语法的主体框架
${X}, X表示变量名称,${X}表示变量值,if语句除外