当前位置:网站首页>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?
边栏推荐
- Knowledge points of MySQL (6)
- CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计
- Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
- Why is all (()) true and any (()) false?
- Mongodb (quick start) (I)
- thinkphp3.2.3
- 如何保存训练好的神经网络模型(pytorch版本)
- [binary tree] insufficient nodes on the root to leaf path
- 使用QT设计师界面类创建2个界面,通过按键从界面1切换到界面2
- Read libco save and restore the on-site assembly code
猜你喜欢
随机推荐
Simple query cost estimation
SQL删除重复数据的实例教程
Debug kernel code through proc interface
一文了解Go语言中的函数与方法的用法
C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
This 17-year-old hacker genius cracked the first generation iPhone!
BigDecimal除法的精度问题
MySQL之知识点(七)
MySQL queries the latest qualified data rows
Customize the theme of matrix (I) night mode
Oracle recovery tools -- Oracle database recovery tool
To solve the problem of "double click PDF file, pop up", please install Evernote program
Thesis reading_ Chinese NLP_ LTP
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
统计php程序运行时间及设置PHP最长运行时间
Ordinary programmers look at the code, and top programmers look at the trend
Oracle Recovery Tools ----oracle数据库恢复利器
求解为啥all(())是True, 而any(())是FALSE?
LeetCode每日一题:合并两个有序数组