cmake学习7--添加环境检查

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16481211/article/details/82259175

有时候可能要对系统环境做点检查,例如要使用一个平台相关的特性的时候。在这个例子中,我们检查系统是否自带 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

在代码中使用宏和函数

#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

猜你喜欢

转载自blog.csdn.net/qq_16481211/article/details/82259175