cmake使用教程(二)-添加库,2021年展望Android原生开发的现状

本文介绍了如何在CMake中添加库,特别是在Android原生开发中的应用。通过示例展示了如何根据用户选择决定是否使用自定义math函数库,并详细解释了CMakeLists.txt的修改过程。
摘要由CSDN通过智能技术生成

if (x <= 0) {
return 0;
}

double result;
double delta;
result = x;

// do ten iterations
int i;
for (i = 0; i < 10; ++i) {
if (result <= 0) {
result = 0.1;
}
delta = x - (result * result);
result = result + 0.5 * delta / result;
fprintf(stdout, “Computing sqrt of %g to be %g\n”, x, result);
}
return result;
}

最后一个更改是将新库添加到可执行文件。根目录下CMakeLists.txt的最后添加以下代码

include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)

add the executable

add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)

现在文件目录如下

.
├── CMakeLists.txt
├── MathFunctions
│   ├── CMakeLists.txt
│   ├── MathFunctions.h
│   └── mysqrt.cxx
├── TutorialConfig.h.in
└── tutorial.cxx

构建可选选项

MathFunctions是我们自己构建的库,有时候我们需要控制这个库是否应该使用,那么可以为使用这个库添加一个开关,在构建大型项目时非常有用。

在项目根目录下的CMakeLists.txt文件中添加如下代码:

should we use our own math functions?

option (USE_MYMATH
“Use tutorial provided math implementation” ON)

假如你使用的是CMake GUI,USE_MYMATH

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

默认值是用户可以根据需要更改。该设置将存储在缓存中,以便用户在每次运行CMake时生成默认配置。然后我们就可以选择性的构建和使用mathfunction库。修改根目录下CMakeLists.txt:

add the MathFunctions library?

if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

add the executable

add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial ${EXTRA_LIBS})

这将使用USE_MYMATH的设置来确定是否应该编译和使用mathfunction库。注意,使用一个变量(在本例中是EXTRA_LIBS)来设置可选的库,然后将它们链接到可执行文件中。这是一种常见的方法,用于保持较大的项目具有许多可选组件。 首先在Configure.h.in文件中添加以下内容:

#cmakedefine USE_MYMATH

然后我们就可以使用USE_MYMATH这个变量了,最后修改Tutorial.cxx源代码如下:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include “TutorialConfig.h”
#ifdef USE_MYMATH
#include “MathFunctions.h”
#endif

int main (int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stdout,"%s Version %d.%d\n", argv[0],
Tutorial_VERSION_MAJOR,
Tutorial_VERSION_MINOR);
fprintf(stdout,“Usage: %s number\n”,argv[0]);
return 1;
}

double inputValue = atof(argv[1]);

#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
#else
double outputValue = sqrt(inputValue);
#endif

fprintf(stdout,“The square root of %g is %g\n”,
inputValue, outputValue);
return 0;
}

我们编译执行看以下结果

  1. 使用自定义的库(USE_MYMATH=ON)

 ~/Desktop/Tutorial/Step2/ ./Tutorial 4
Computing sqrt of 4 to be 2.5
Computing sqrt of 4 to be 2.05
Computing sqrt of 4 to be 2.00061
Computing sqrt of 4 to be 2
Computing sqrt of 4 to be 2
Computing sqrt of 4 to be 2
Computing sqrt of 4 to be 2
Computing sqrt of 4 to be 2
Computing sqrt of 4 to be 2
Computing sqrt of 4 to be 2
The square root of 4 is 2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值