当前位置:网站首页>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 .
边栏推荐
- 启牛商学院股票开户安全吗?靠谱吗?
- Deeply cultivate 5g, and smart core continues to promote 5g applications
- Use of ThinkPHP template
- CMake教程Step4(安装和测试)
- MySQL queries the latest qualified data rows
- Flow characteristics of kitchen knife, ant sword, ice scorpion and Godzilla
- MYSQL group by 有哪些注意事项
- Embedded -arm (bare board development) -1
- thinkphp3.2.3
- 华为云云原生容器综合竞争力,中国第一!
猜你喜欢
Tips for extracting JSON fields from MySQL

winedt常用快捷键 修改快捷键latex编译按钮
Redis+caffeine two-level cache enables smooth access speed
MySql 查询符合条件的最新数据行

一个满分的项目文档是如何书写的|得物技术

7.Scala类

7. Scala class

WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
In depth understanding of redis memory obsolescence strategy

33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
随机推荐
mysql如何使用JSON_EXTRACT()取json值
Rider 设置选中单词侧边高亮,去除警告建议高亮
Detailed explanation of printf() and scanf() functions of C language
The second day of learning C language for Asian people
Function sub file writing
Using C language to realize palindrome number
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
MYSQL group by 有哪些注意事项
张平安:加快云上数字创新,共建产业智慧生态
Embedded-c language-6
干货!半监督预训练对话模型 SPACE
First day of learning C language
Thoughtworks 全球CTO:按需求构建架构,过度工程只会“劳民伤财”
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
Embedded -arm (bare board development) -1
Complete solution instance of Oracle shrink table space
CMake教程Step3(添加库的使用要求)
Mysql5.6 parsing JSON strings (supporting complex nested formats)
Oracle缩表空间的完整解决实例
通过proc接口调试内核代码