当前位置:网站首页>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 .
边栏推荐
猜你喜欢
What are the precautions for MySQL group by
Summary of optimization scheme for implementing delay queue based on redis
Example tutorial of SQL deduplication
深入理解Redis内存淘汰策略
MYSQL group by 有哪些注意事项

Embedded-c Language-1
Learn about MySQL transaction isolation level

7.Scala类

Wsl2.0 installation

thinkphp模板的使用
随机推荐
Debug kernel code through proc interface
Application of threshold homomorphic encryption in privacy Computing: Interpretation
漫画:如何实现大整数相乘?(整合版)
Using C language to realize palindrome number
ECU introduction
Use of ThinkPHP template
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
激动人心!2022开放原子全球开源峰会报名火热开启!
33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
漫画:寻找无序数组的第k大元素(修订版)
About JSON parsing function JSON in MySQL_ EXTRACT
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
How to write a full score project document | acquisition technology
漫画:有趣的【海盗】问题
基于Redis实现延时队列的优化方案小结
一个满分的项目文档是如何书写的|得物技术
ternary operator
网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
[Jianzhi offer] 63 Maximum profit of stock