CMake入门实战——其他

时间:2022-07-22
本文章向大家介绍CMake入门实战——其他,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

支持 gdb

让 CMake 支持 gdb 的设置也很容易,只需要指定 Debug 模式下开启 -g 选项:

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

之后可以直接对生成的程序使用 gdb 来调试。

添加环境检查

有时候可能要对系统环境做点检查,例如要使用一个平台相关的特性的时候。在这个例子中,我们检查系统是否自带 pow 函数。如果带有 pow 函数,就使用它;否则使用我们定义的 power 函数。

添加 CheckFunctionExists 宏

首先在顶层 CMakeLists 文件中添加 CheckFunctionExists.cmake 宏,并调用 check_function_exists 命令测试链接器是否能够在链接阶段找到 pow 函数。

# 检查系统是否支持 pow 函数
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
check_function_exists (pow HAVE_POW)

将上面这段代码放在 configure_file 命令前。

预定义相关宏变量

接下来修改 config.h.in 文件,预定义相关的宏变量。

// does the platform provide pow function?
#cmakedefine HAVE_POW

在代码中使用宏和函数

最后一步是修改 main.cc ,在代码中使用宏和函数:

#ifdef HAVE_POW
    printf("Now we use the standard library. n");
    double result = pow(base, exponent);
#else
    printf("Now we use our own Math library. n");
    double result = power(base, exponent);
#endif

添加版本号

给项目添加和维护版本号是一个好习惯,这样有利于用户了解每个版本的维护情况,并及时了解当前所用的版本是否过时,或是否可能出现不兼容的情况。

首先修改顶层 CMakeLists 文件,在 project 命令之后加入如下两行:

set (Demo_VERSION_MAJOR 1)
set (Demo_VERSION_MINOR 0)

分别指定当前的项目的主版本号和副版本号。 之后,为了在代码中获取版本信息,我们可以修改 config.h.in 文件,添加两个预定义变量:

// the configured options and settings for Tutorial
#define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@
#define Demo_VERSION_MINOR @Demo_VERSION_MINOR@

这样就可以直接在代码中打印版本信息了:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "config.h"
#include "math/MathFunctions.h"
int main(int argc, char *argv[])
{
    if (argc < 3){
        // print version info
        printf("%s Version %d.%dn",
            argv[0],
            Demo_VERSION_MAJOR,
            Demo_VERSION_MINOR);
        printf("Usage: %s base exponent n", argv[0]);
        return 1;
    }
    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);

#if defined (HAVE_POW)
    printf("Now we use the standard library. n");
    double result = pow(base, exponent);
#else
    printf("Now we use our own Math library. n");
    double result = power(base, exponent);
#endif

    printf("%g ^ %d is %gn", base, exponent, result);
    return 0;
}

测试的结果:

xuke@ubuntu:~/work/cmake-demo/Demo7$ ls
CMakeLists.txt  config.h.in  main.cc  math
xuke@ubuntu:~/work/cmake-demo/Demo7$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.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
-- Looking for pow
-- Looking for pow - not found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xuke/work/cmake-demo/Demo7
xuke@ubuntu:~/work/cmake-demo/Demo7$ make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunctions.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo
[ 75%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
xuke@ubuntu:~/work/cmake-demo/Demo7$ ./Demo 3 2
Now we use our own Math library.
3 ^ 2 is 9
xuke@ubuntu:~/work/cmake-demo/Demo7$ ./Demo 3
./Demo Version 1.0
Usage: ./Demo base exponent

参考

[CMake 入门实战] http://www.hahack.com/codes/cmake/ [代码参考] https://github.com/wzpan/cmake-demo