当前位置:网站首页>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 .
边栏推荐
- [Jianzhi offer] 61 Shunzi in playing cards
- Embedded -arm (bare board development) -2
- [Jianzhi offer] 63 Maximum profit of stock
- IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!
- 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
- Thoughtworks 全球CTO:按需求构建架构,过度工程只会“劳民伤财”
- 关于mysql中的json解析函数JSON_EXTRACT
- Mysql5.6 parsing JSON strings (supporting complex nested formats)
- 【testlink】TestLink1.9.18常见问题解决方法
- 忽米沄析:工业互联网标识解析与企业信息系统的融合应用
猜你喜欢

thinkphp模板的使用
Learn about MySQL transaction isolation level

7. Scala class

Deeply cultivate 5g, and smart core continues to promote 5g applications
Tips for extracting JSON fields from MySQL

WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands

干货!半监督预训练对话模型 SPACE
一文了解MySQL事务隔离级别

【性能测试】jmeter+Grafana+influxdb部署实战
In depth understanding of redis memory obsolescence strategy
随机推荐
CMake教程Step6(添加自定义命令和生成文件)
dried food! Semi supervised pre training dialogue model space
漫画:一道数学题引发的血案
VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在
Etcd build a highly available etcd cluster
蚂蚁金服的暴富还未开始,Zoom的神话却仍在继续!
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
mysql如何使用JSON_EXTRACT()取json值
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
漫画:有趣的海盗问题 (完整版)
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
NPM installation
这个17岁的黑客天才,破解了第一代iPhone!
[7.7 live broadcast preview] the lecturer of "typical architecture of SaaS cloud native applications" teaches you to easily build cloud native SaaS applications. Once the problem is solved, Huawei's s
Embedded -arm (bare board development) -2
Function sub file writing
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword