title: CMake实战三:自定义编译选项

categories:[实战三]

tags:[CMake]

date: 2021/12/24

作者:hackett

微信公众号:加班猿


CMake 容许为我的项目减少编译选项,从而能够依据用户的环境和需要抉择最合适的编译计划。

很多开源库都会有CMake来进行治理编译,比方亚马逊AWS的WebRTC中的CMake外面有这么一行

option(USE_OPENSSL "Use openssl as crypto library" ON)

ON示意应用openssl的库,OFF示意不应用openssl的库

1、批改CMakeLists文件

# 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})

其中:

  1. configure_file 命令用于退出一个配置头文件 config.h ,这个文件由 CMake 从 config.h.in 生成,通过这样的机制,将能够通过预约义一些参数和变量来控制代码的生成。
  2. option 命令增加了一个 USE_MYMATH 选项,并且默认值为 ON
  3. USE_MYMATH 变量的值来决定是否应用咱们本人编写的 MathFunctions 库。

2、批改main.cpp文件

让其依据 USE_MYMATH 的预约义值来决定是否调用规范库还是 MathFunctions 库:

#include <stdio.h>#include <stdlib.h>#include "config.h"#ifdef USE_MYMATH  #include "math/myMath.h"#else  #include <math.h>#endifint sub(int a, int b) {    return (a - b);}int main(int argc, char *argv[]) {    if (argc < 3){        printf("Usage: %s base exponent \n", argv[0]);        return 1;    }    int a = atof(argv[1]);    int b = atoi(argv[2]);    #ifdef USE_MYMATH    printf("Now we use our own Math library. \n");    int result = add(a, b);    printf("%d + %d = %d\n", a, b, result);#else    printf("Now we use the main.cpp sub function. \n");    int result = sub(a, b);    printf("%d - %d = %d\n", a, b, result);#endif    return 0;}

3、编写config.h.in文件

下面的程序值得注意的是第2行,这里援用了一个 config.h 文件,这个文件预约义了 USE_MYMATH 的值。但咱们并不间接编写这个文件,为了不便从 CMakeLists.txt 中导入配置,咱们编写一个 config.h.in 文件,内容如下:

#cmakedefine USE_MYMATH

这样 CMake 会主动依据 CMakeLists 配置文件中的设置主动生成 config.h 文件。

4、编译我的项目

option (USE_MYMATH "Use provided math implementation" ON)

运行后果:

[root@hackett demo4]# cmake .-- The C compiler identification is GNU 8.4.1-- The CXX compiler identification is GNU 8.4.1-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Check for working C compiler: /usr/bin/cc - skipped-- Detecting C compile features-- Detecting C compile features - done-- Detecting CXX compiler ABI info-- Detecting CXX compiler ABI info - done-- Check for working CXX compiler: /usr/bin/c++ - skipped-- Detecting CXX compile features-- Detecting CXX compile features - done-- Configuring done-- Generating done-- Build files have been written to: /root/workspace/cmake/demo4[root@hackett demo4]# cat config.h#define USE_MYMATH[root@hackett demo4]# makeConsolidate compiler generated dependencies of target MathFunctions[ 50%] Built target MathFunctionsConsolidate compiler generated dependencies of target demo[ 75%] Building CXX object CMakeFiles/demo.dir/main.cpp.o[100%] Linking CXX executable demo[100%] Built target demo[root@hackett demo4]# ./demo 3 2Now we use our own Math library. 3 + 2 = 5

option (USE_MYMATH "Use provided math implementation" OFF)

运行后果:

[root@hackett demo4]# cmake .-- The C compiler identification is GNU 8.4.1-- The CXX compiler identification is GNU 8.4.1-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Check for working C compiler: /usr/bin/cc - skipped-- Detecting C compile features-- Detecting C compile features - done-- Detecting CXX compiler ABI info-- Detecting CXX compiler ABI info - done-- Check for working CXX compiler: /usr/bin/c++ - skipped-- Detecting CXX compile features-- Detecting CXX compile features - done-- Configuring done-- Generating done-- Build files have been written to: /root/workspace/cmake/demo4[root@hackett demo4]# cat config.h/* #undef USE_MYMATH */[root@hackett demo4]# make[ 50%] Building CXX object CMakeFiles/demo.dir/main.cpp.o[100%] Linking CXX executable demo[100%] Built target demo[root@hackett demo4]# ./demo  3 2Now we use the main.cpp sub function. 3 - 2 = 1

如果你感觉文章还不错,能够给个"三连",文章同步到集体微信公众号[加班猿]

我是hackett,咱们下期见

参考文档:

CMake入门实战

CMake Tutorial