当前位置:网站首页>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)
- Machine learning 02: model evaluation
- Webapp development - Google official tutorial
- 独立开发,不失为程序员的一条出路
- Cartoon: looking for the best time to buy and sell stocks
- Mysql5.6 parsing JSON strings (supporting complex nested formats)
- Tita performance treasure: how to prepare for the mid year examination?
- [binary tree] insufficient nodes on the root to leaf path
- Disabling and enabling inspections pycharm
- Oracle recovery tools -- Oracle database recovery tool
猜你喜欢
Database design in multi tenant mode
Design of electronic clock based on 51 single chip microcomputer
Seven Devops practices to improve application performance
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
CVPR 2022 best student paper: single image estimation object pose estimation in 3D space
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
C # mixed graphics and text, written to the database in binary mode
Why is all (()) true and any (()) false?
7 pratiques devops pour améliorer la performance des applications
MySQL之知识点(七)
随机推荐
Seven Devops practices to improve application performance
Disabling and enabling inspections pycharm
thinkphp3.2.3
Knowledge points of MySQL (6)
毫无章法系列
Why is all (()) true and any (()) false?
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
Database design in multi tenant mode
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Anaconda中配置PyTorch环境——win10系统(小白包会)
7. Scala class
基于YOLOv3的口罩佩戴检测
一口气读懂 IT发展史
Read the history of it development in one breath
Example tutorial of SQL deduplication
Sentinel-流量防卫兵
请问下为啥有的表写sql能查到数据,但在数据地图里查不到啊,查表结构也搜不到
Disorganized series
WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands