cmake入门系列总结四
原创
©著作权归作者所有:来自51CTO博客作者逍遥游10的原创作品,请联系作者获取转载授权,否则将追究法律责任
cmake入门系列总结四
__
版本说明
版本
| 作者
| 日期
| 备注
|
0.1
| loon
| 2019.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
result = exp(log(x)*0.5);
#else// otherwise use an iterative approach
. . .
二、具体目录结构和演示
这里由于需要使用一些数学函数,所以我们之前的helloWorld测试例子无法使用了,这里我们就实现一下官网给出的例子。
目录结构:
zy@zy-virtual-machine:~/test/cmake_test/tutorial$ tree .
.
tutorial.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
int main(int argc, char *argv[])
{
float result = -3;
#if
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%]
三、最后
我这里发现了一个问题:1、我之前将生成的头文件位置指定到了PROJECT_SOURCE_DIR下,重新编译时总是要手动删掉,今天我终于发现了指定的目录错误了,应该是PROJECT_BINARY_DIR这个位置。(之前的错误就放在那里当做一个坑来警醒一下自己要仔细,踩到坑的也不要骂我啊)2、生成的配置文件总是如下所示:
/* #undef HAVE_LOG */
/* #undef HAVE_EXP */
不知道这是什么意思呐?暂时埋在这。