当前位置:网站首页>Cmake tutorial step5 (add system self-test)
Cmake tutorial step5 (add system self-test)
2022-07-05 17:45:00 【It's beginning to boil】
CMake Official documents
Refer to the official cmake3.24 Course translation . I'm going to use cmake 3.16 To demonstrate examples .
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
My warehouse :
https://github.com/FRBoiling/cmake-tutorial.git
Let's consider adding some code to the project that depends on features that the target platform may not have .
For this example , We will add some that depend on whether the target platform has log and exp Function code . Of course , Almost every platform has these functions , But assume in this tutorial that they are not common .
If the platform has log and exp, Then we will use them in mysqrt Calculate the square root in the function .
We use MathFunctions/CMakeLists.txt Medium CheckCXXSourceCompiles Module to test the usability of these functions .
Calling target_include_directories() after , take log and exp The check of is added to 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)
If available , Use target_compile_definitions() take HAVE_LOG and HAVE_EXP Specify private compilation definitions .
if(HAVE_LOG AND HAVE_EXP)
target_compile_definitions(MathFunctions
PRIVATE "HAVE_LOG" "HAVE_EXP")
endif()
If log and exp Available on the system , Then we will use them in mysqrt Calculate the square root in the function . Add the following code to math_functions/mysqrt.cxx Medium mysqrt Function ( Don't forget before returning the result #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;
We still need to revise it mysqrt.cxx contain cmath The header file .
#include <cmath>
Come here ,mysqrt.cxx The content is long like this :
#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;
}
function cmake Executable or cmake-gui To configure the project , Then build it using the build tool of your choice , And run the tutorial executable .
test
Now which function gives a better result ,sqrt still mysqrt?
边栏推荐
- Flow characteristics of kitchen knife, ant sword, ice scorpion and Godzilla
- [binary tree] insufficient nodes on the root to leaf path
- 一文读懂简单查询代价估算
- 十个顶级自动化和编排工具
- 漫画:有趣的海盗问题 (完整版)
- 漫画:寻找股票买入卖出的最佳时机
- Seven Devops practices to improve application performance
- LeetCode每日一题:合并两个有序数组
- Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
- Tita 绩效宝:如何为年中考核做准备?
猜你喜欢
Oracle缩表空间的完整解决实例

Anaconda中配置PyTorch环境——win10系统(小白包会)

网络威胁分析师应该具备的十种能力

神经网络自我认知模型

求解为啥all(())是True, 而any(())是FALSE?

CVPR 2022 best student paper: single image estimation object pose estimation in 3D space

Thesis reading_ Medical NLP model_ EMBERT

Count the running time of PHP program and set the maximum running time of PHP

33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)

Machine learning 02: model evaluation
随机推荐
Abnormal recovery of virtual machine Oracle -- Xi Fenfei
Count the running time of PHP program and set the maximum running time of PHP
一文了解Go语言中的函数与方法的用法
毫无章法系列
得知女儿被猥亵,35岁男子将对方打至轻伤二级,法院作出不起诉决定
读libco保存恢复现场汇编代码
Is it safe to open an account online? What is the general interest rate of securities financing?
Flask solves the problem of CORS err
查看自己电脑连接过的WiFi密码
Cartoon: how to multiply large integers? (next)
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
如何保存训练好的神经网络模型(pytorch版本)
Disabling and enabling inspections pycharm
解读:如何应对物联网目前面临的安全问题?
Thesis reading_ Chinese NLP_ LTP
Accuracy of BigDecimal Division
tkinter窗口预加载
漫画:有趣的海盗问题 (完整版)
統計php程序運行時間及設置PHP最長運行時間
VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在