开发者

从零学习cmake构建系统

目录
  • 什么是cmake
  • 入门实践
    • 构建单个源文件
    • 同一个目录,多个源文件
    • 多个目录多个源文件
    • 自定义编译选项
  • 最后

    什么是cmake

    根据其官网的定义,cmake是一个开源跨平台的,用于构建、测试和打包软件的一个套件工具。与cmake相关的几个概念需要明晰,如GCC,make和makefile。

    • GCC:GCC是GNU Compiler Collection(就是GNU编译器套件),也可以简单认为是编译器,它可以编译很多种编程语言(括C、C++、Objective-C、Fortran、Java等等)
    • make:make工具可以看成是一个智能的批处理工具,它本身并没有编译和链接的功能,而是用类似于批处理的方式通过调用makefile文件中用户指定的命令来进行编译和链接的。
    • makefile,makefile就是GCC命令的集合,make工具就根据makefile中的命令进行编译和链接的
    • cmake,当项目非常大时,手写makefile会非常烦碎,而且不同平台makefile也会不同,所以cmake就是可以根据CMakeLists.txt自动生成makefile。

    入门实践

    我们已经对于cmake有了一个总体上的认识,接下来就通过官方提供的几个例子介绍下如何使用cmake。

    构建单个源文件

    项目只有一个源文件,结构如下,我们一般会新建一个目录存储cmake运行的产物,这里我新建了一个build目录。

    ├── CMakeLists.txt
    ├── build
    └── main.cc
    
    gKCTtU

    main.cc的内容如下

    #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的内容如下:

    # Cmake的最低版本号的要求
    cmake_minimum_required (VERSION 2.8)
    # 项目信息
    project (Demo1)
    # 指定生成目标
    add_executable(Demo main.cc)
    

    在buihttp://www.devze.comld目录下执行cmake .. 然后执行make命令就可以得到Demo1的可执行文件。

    同一个目录,多个源文件

    如果我们将Demo1中main.cc的power函数抽取出来放到MathFunctions.cc中,项目结构如下:

    ├── CMakeLists.txt
    ├── MathFunctions.cc
    ├── MathFunctions.h
    ├── build
    └── main.cc
    

    那么该如何编译呢,我们可以通过在add_executable命令中增加将MathFunctions.cc,效果如下add_executable(Demo main.cc MathFunctions.cc),但是如果有很多文件的情况下,一个文件一个文件的添加很麻烦,cmake提供了aux_source_directory命令,该命令会查找指定目录下所有的源文件,然后将结果存到指定的变量名。CMakeLists.txt文件内容如下

    # CMake 最低版本号要求
    cmake_minimum_required (VERSION 2.8)
    # 项目信息
    project (Demo2)
    # 查找目录下的所有源文件
    # 并将名称保存到 DIR_SRCS 变量
    aux_source_directory(. DIR_SRCS)
    # 指定生成目标
    add_executable(Demo ${DIR_SRCS})
    

    多个http://www.devze.com目录多个源文件

    现在我们将MathFounction.h和MathFounction.cpp移动到math目录下,项目结构如下:

    ├── CMakeLists.txt
    ├── build
    ├── main.cc
    └── math
        ├── CMakeLists.txt
        ├── MathFunctions.cc
        └── MathFunction编程s.h
    

    面对这种情况我们需要在Demo3目录下和math目录下各自编写一个CmakeLists.txt文件,我们可以将math目录里面的文件编译成静态库再由main函数调用。 Demo3目录下的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 ${DIR_SRCS})
    # 添加链接库
    target_link_libraries(Demo MathFunctions)
    

    add_subdictory(math)指明本项目包含一个子目录math,这样,math目录下的CMakeLists.txt文件和源代码也会被使用,最后一行target_link_libraries指明可执行文件需要链接一个名为MathFunctions的链接库。math目录下CMakeLists.txt内容如下

    # 查找当前目录下的所有源文件
    # 并将名称保存到 DIR_LIB_SRCS 变量
    aux_source_directory(. DIR_LIB_SRCS)
    # 指定生成 MathFunctions 链接库
    add_library (MathFunctions ${DIR_LIB_SRCS})
    

    该文件中使用命令add_library将src目录中源文件编译为静态链接库

    自定义编译选项

    CMake允许为项目增加编译选项,从而可以根据用户的环境和要求选择最合适的编译方案,例如可以将MathFunctions库设置为一个可选的库,如果该选项为ON,则使用该库定义的函数来进行运算,否则就调用标准库的数学函数库。 为了实现这样的目的我们需要在顶层的CMakeLists.txt中添加该选项,其内容如下

    cmake_minimum_required (VERSION 2.8)
    # 项目信息
    project (Demo4)
    set(CMAKEjs_INCLUDE_CURRENT_DIR ON)
    # 是否使用自己的 MathFunctions 库
    option (USE_MYMATH
    	   "Use provided math implementation" ON)
    # 加入一个配置头文件,用于处理 CMake 对源码的设置
    configure_file (
      "${PROJECT_SOURCE_DIR}/config.h.in"
      "${PROJECT_BINARY_DIR}/config.h"
      )
    # 是否加入 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})
    

    其中configure_file命令用于加入一个配置文件config.h,这个文件由CMake从config.h.in生成,通过这样的机制可以通过预定义一些参数和变量来控制代码的生成,config.h.in内容如下:

    #cmakedefine USE_MYMATH
    

    其中的option命令添加了一个USE_MYMATH选项,并且默认值为ON。之后根据USE_MYMATH变量的值决定是否使用我门自己编写的MathFounctions库

    然后需要更改main.cc文件让其根据USE_MYMATH的值确定是否调用标准库,内容如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <config.h>
    #ifdef USE_MYMATH
      #include <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);
    #e开发者_Python学习ndif
        printf("%g ^ %d is %g\n", base, exponent, result);
        return 0;
    }
    

    值得注意的是这里引入了一个config.h,这个文件预定义了 USE_MYMATH 的值。但我们并不直接编写这个文件,而是由CMake根据config.h.in自动生成。

    最后

    这篇文章主要介绍了cmake的简单使用,更多关于cmake构建系统的资料请关注我们其它相关文章!

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜