当前位置:网站首页>Cmake tutorial step6 (add custom commands and generate files)
Cmake tutorial step6 (add custom commands and generate files)
2022-07-05 17:24: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
step6
https://cmake.org/cmake/help/v3.24/guide/tutorial/Adding%20a%20Custom%20Command%20and%20Generated%20File.html
My warehouse :
https://github.com/FRBoiling/cmake-tutorial.git
hypothesis , For the purposes of this tutorial , We decided never to use the platform log and exp function , Instead, you need to generate a table of pre calculated values , be used for mysqrt function .
In this section , We will create tables as part of the build process , Then compile the table into our application .
initialization
First , Let's delete MathFunctions/CMakeLists.txt in log and exp Function check .
add_library(MathFunctions mysqrt.cxx)
# state that anybody linking to us needs to include the current source dir to find MathFunctions.h, while we don't.
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
# install rules
install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)
then , from mysqrt.cxx Delete the right HAVE_LOG and HAVE_EXP The inspection of . meanwhile , We can delete #include .
double mysqrt(double x)
{
if (x <= 0)
{
return 0;
}
// #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;
}
stay MathFunctions Directory , newly added MakeTable.cxx The source file , Used to generate table. We can see table As valid c++ Code generated , And the output file name is passed in as a parameter .
// A simple program that builds a sqrt table
#include <cmath>
#include <fstream>
#include <iostream>
int main(int argc, char* argv[])
{
// make sure we have enough arguments
if (argc < 2) {
return 1;
}
std::ofstream fout(argv[1], std::ios_base::out);
const bool fileOpen = fout.is_open();
if (fileOpen) {
fout << "double sqrtTable[] = {" << std::endl;
for (int i = 0; i < 10; ++i) {
fout << sqrt(static_cast<double>(i)) << "," << std::endl;
}
// close the table with a zero
fout << "0};" << std::endl;
fout.close();
}
return fileOpen ? 0 : 1; // return 0 if wrote the file
}
Custom command generation
The next step is to MathFunctions/CMakeLists.txt Add the appropriate command to the file , In order to build MakeTable Executable file , Then run it as part of the build process . Several commands are required to complete this operation .
First , stay MathFunctions/CMakeLists.txt At the top of the , add to MakeTable The executable of
add_executable(MakeTable MakeTalbe.cxx)
Then add a custom command , Specify how to run MakeTable Generate table.h
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
DEPENDS MakeTable
)
Next we will let CMake know mysqrt.cxx Depends on the generated file table.h. This is generated by table.h Add to standard library MathFunctions Source list of .
add_library(MathFunctions
mysqrt.cxx
${CMAKE_CURRENT_BINARY_DIR}/Table.h
)
We must also add the current binary directory to include Directory list , In order to mysqrt.cxx Be able to find and contain table.h.
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
)
Use the generated table
First , modify mysqrt.cxx contain table.h. Next , We can rewrite mysqrt Function to use this table :
double mysqrt(double x)
{
if (x <= 0) {
return 0;
}
// use the table to help find an initial value
double result = x;
if (x >= 1 && x < 10) {
std::cout << "Use the table to help find an initial value " << std::endl;
result = sqrtTable[static_cast<int>(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;
}
return result;
}
Test practice
function cmake Executable or cmake-gui To configure the project , Then build it using the build tool of your choice .
When the project is built , It will first build MakeTable Executable file . And then run MakeTable Generate table.h. Last , It will compile mysqrt.cxx, These include table.h To generate MathFunctions library .
function Tutorial Executable and verify that it uses tables .
边栏推荐
- WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
- 漫画:一道数学题引发的血案
- 菜刀,蚁剑,冰蝎,哥斯拉的流量特征
- The second day of learning C language for Asian people
- C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
- Use of ThinkPHP template
- Browser rendering principle and rearrangement and redrawing
- 腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
- 华为云云原生容器综合竞争力,中国第一!
- Summary of optimization scheme for implementing delay queue based on redis
猜你喜欢
High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body
Embedded-c Language-1
项目引入jar从私服Nexus 拉去遇到的一个问题
[Web attack and Defense] WAF detection technology map
Learn about MySQL transaction isolation level
[Jianzhi offer] 63 Maximum profit of stock
Embedded UC (UNIX System Advanced Programming) -1
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
ternary operator
Machine learning 02: model evaluation
随机推荐
Using C language to realize palindrome number
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
First day of learning C language
Embedded-c Language-5
华为云云原生容器综合竞争力,中国第一!
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
CMake教程Step6(添加自定义命令和生成文件)
[wechat applet] read the life cycle and route jump of the applet
蚂蚁金服的暴富还未开始,Zoom的神话却仍在继续!
Browser rendering principle and rearrangement and redrawing
忽米沄析:工业互联网标识解析与企业信息系统的融合应用
[Jianzhi offer] 66 Build product array
Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
Is it safe and reliable to open futures accounts on koufu.com? How to distinguish whether the platform is safe?
Redis+caffeine two-level cache enables smooth access speed
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
这个17岁的黑客天才,破解了第一代iPhone!
网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
In depth understanding of redis memory obsolescence strategy