当前位置:网站首页>CMake教程Step5(添加系统自检)
CMake教程Step5(添加系统自检)
2022-07-05 16:36:00 【开始沸腾了】
CMake官方文档
参考官方cmake3.24教程翻译。我这里使用cmake 3.16来演示例子。
https://cmake.org/cmake/help/v3.24/guide/tutorial/index.html
https://gitlab.kitware.com/cmake/cmake/-/tree/master/Help/guide/tutorial
step5
https://cmake.org/cmake/help/v3.24/guide/tutorial/Adding%20System%20Introspection.html
我的仓库 :
https://github.com/FRBoiling/cmake-tutorial.git
让我们考虑向项目中添加一些依赖于目标平台可能不具备的特性的代码。
对于本例,我们将添加一些依赖于目标平台是否具有log和exp函数的代码。当然,几乎每个平台都有这些功能,但在本教程中假设它们并不常见。
如果平台有log和exp,那么我们将使用它们在mysqrt函数中计算平方根。
我们首先使用MathFunctions/CMakeLists.txt中的CheckCXXSourceCompiles模块来测试这些函数的可用性。
在调用target_include_directories()之后,将log和exp的检查添加到MathFunctions/CMakeLists.txt:
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
# does this system provide the log and exp functions?
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
#include <cmath>
int main() {
std::log(1.0);
return 0;
}
" HAVE_LOG)
check_cxx_source_compiles("
#include <cmath>
int main() {
std::exp(1.0);
return 0;
}
" HAVE_EXP)
如果可用,使用target_compile_definitions()将HAVE_LOG和HAVE_EXP指定为私有编译定义。
if(HAVE_LOG AND HAVE_EXP)
target_compile_definitions(MathFunctions
PRIVATE "HAVE_LOG" "HAVE_EXP")
endif()
如果log和exp在系统上可用,那么我们将使用它们在mysqrt函数中计算平方根。将以下代码添加到math_functions/mysqrt.cxx中的mysqrt函数中(在返回结果之前不要忘记#endif):
#if defined(HAVE_LOG) && defined(HAVE_EXP)
double result = std::exp(std::log(x) * 0.5);
std::cout << "Computing sqrt of " << x << " to be " << result
<< " using log and exp" << std::endl;
#else
double result = x;
我们还需要修改mysqrt.cxx包含cmath头文件。
#include <cmath>
到这里,mysqrt.cxx内容长这样:
#include <cmath>
#include <iostream>
#include "MathFunctions.h"
// a hack square root calculation using simple operations
double mysqrt(double x)
{
if (x <= 0) {
return 0;
}
// if we have both log and exp then use them
#if defined(HAVE_LOG) && defined(HAVE_EXP)
double result = std::exp(std::log(x) * 0.5);
std::cout << "Computing sqrt of " << x << " to be " << result
<< " using log and exp" << std::endl;
#else
double result = x;
// do ten iterations
for (int i = 0; i < 10; ++i) {
if (result <= 0) {
result = 0.1;
}
double delta = x - (result * result);
result = result + 0.5 * delta / result;
std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
}
#endif
return result;
}
运行cmake可执行文件或cmake-gui来配置项目,然后使用您选择的构建工具构建它,并运行教程可执行文件。
测试
现在哪个函数给出了更好的结果,sqrt还是mysqrt?
边栏推荐
- ECU introduction
- SQL injection of cisp-pte (Application of secondary injection)
- Combined use of vant popup+ other components and pit avoidance Guide
- Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
- 叩富网开期货账户安全可靠吗?怎么分辨平台是否安全?
- 项目引入jar从私服Nexus 拉去遇到的一个问题
- [Web attack and Defense] WAF detection technology map
- 网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
- Facing new challenges and becoming a better self -- attacking technology er
- Embedded-c Language-2
猜你喜欢
WSL2.0安装
【729. 我的日程安排錶 I】
[Web attack and Defense] WAF detection technology map
【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
7.Scala类
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
[Jianzhi offer] 63 Maximum profit of stock
Jarvis OJ shell traffic analysis
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
Games101 notes (II)
随机推荐
启牛商学院股票开户安全吗?靠谱吗?
Embedded-c Language-2
【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
Bs-xx-042 implementation of personnel management system based on SSM
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
挖财股票开户安全吗?怎么开股票账户是安全?
Etcd build a highly available etcd cluster
PHP talent recruitment system development source code recruitment website source code secondary development
[brush questions] effective Sudoku
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Allusions of King Xuan of Qi Dynasty
【机器人坐标系第一讲】
It is forbidden to copy content JS code on the website page
Writing method of twig array merging
C# TCP如何设置心跳数据包,才显得优雅呢?
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
Wechat official account web page authorization login is so simple
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
手机开证券账户安全吗?怎么买股票详细步骤