cmake入门系列总结四

cmake入门系列总结四

__

版本说明

版本作者日期备注
0.1loon2019.3.14初稿

目录

一、添加系统自省

接下来让我们考虑在项目中添加一些代码,这些代码取决于目标平台可能没有的功能。对于此示例,我们将添加一些代码,这些代码取决于目标平台是否具有log和exp函数。当然,几乎每个平台都有这些功能,但本教程假设它们不太常见。如果平台有日志,那么我们将使用它来计算mysqrt函数中的平方根。我们首先使用顶级CMakeLists.txt文件中的CheckFunctionExists.cmake宏测试这些函数的可用性,如下所示:

# does this system provide the log and exp functions?
include (CheckFunctionExists)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

接下来我们修改TutorialConfig.h.in以定义这些值,如果CMake在平台上找到它们,如下所示:

// does the platform provide exp and log functions?
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP

在TutorialConfig.h的configure_file命令之前完成log和exp的测试非常重要。configure_file命令使用CMake中的当前设置立即配置文件。最后在mysqrt函数中,如果使用以下代码在系统上可用,我们可以提供基于log和exp的备用实现:

// if we have both log and exp then use them
#if defined (HAVE_LOG) && defined (HAVE_EXP)
  result = exp(log(x)*0.5);
#else // otherwise use an iterative approach
  . . .

二、具体目录结构和演示

这里由于需要使用一些数学函数,所以我们之前的helloWorld测试例子无法使用了,这里我们就实现一下官网给出的例子。

目录结构:

zy@zy-virtual-machine:~/test/cmake_test/tutorial$ tree .
.
├── build
├── CMakeLists.txt
├── TutorialConfig.h.in
└── tutorial.cxx

1 directory, 3 files

tutorial.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"

int main(int argc, char *argv[])
{
    float result = -3;
#if defined (HAVE_LOG) && defined (HAVE_EXP)
    result = exp(log(x) * 0.5);
    printf("result:%f\n", result);
#else
    printf("result:%f\n", result);
#endif

    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project (Tutorial)

configure_file(
    "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
    "${PROJECT_BINARY_DIR}/TutorialConfig.h"
)

include_directories("${PROJECT_BINARY_DIR}")

include(CheckFunctionExists)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

add_executable(Tutorial tutorial.cxx)

执行过程:

zy@zy-virtual-machine:~/test/cmake_test/tutorial$ cd build/
zy@zy-virtual-machine:~/test/cmake_test/tutorial/build$ ls
zy@zy-virtual-machine:~/test/cmake_test/tutorial/build$ 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 log
-- Looking for log - not found
-- Looking for exp
-- Looking for exp - not found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zy/test/cmake_test/tutorial/build
zy@zy-virtual-machine:~/test/cmake_test/tutorial/build$ make
Scanning dependencies of target Tutorial
[ 50%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.o
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
zy@zy-virtual-machine:~/test/cmake_test/tutorial/build$ ./Tutorial 
result:-3.000000

三、最后

我这里发现了一个问题:1、我之前将生成的头文件位置指定到了PROJECT_SOURCE_DIR下,重新编译时总是要手动删掉,今天我终于发现了指定的目录错误了,应该是PROJECT_BINARY_DIR这个位置。(之前的错误就放在那里当做一个坑来警醒一下自己要仔细,踩到坑的也不要骂我啊)2、生成的配置文件总是如下所示:

/* #undef HAVE_LOG */
/* #undef HAVE_EXP */

不知道这是什么意思呐?暂时埋在这。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昵称系统有问题

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值