当前位置:网站首页>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
- Function sub file writing
- Machine learning 02: model evaluation
- Matery主题自定义(一)黑夜模式
- CMake教程Step5(添加系统自检)
- WebApp开发-Google官方教程
- Error in composer installation: no composer lock file present.
- stirring! 2022 open atom global open source summit registration is hot!
- C # realizes crystal report binding data and printing 3-qr code barcode
- Debug kernel code through proc interface
猜你喜欢

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

Etcd build a highly available etcd cluster

Embedded -arm (bare board development) -2

Deeply cultivate 5g, and smart core continues to promote 5g applications

Machine learning compilation lesson 2: tensor program abstraction

CMake教程Step2(添加库)

How to write a full score project document | acquisition technology

dried food! Semi supervised pre training dialogue model space

【性能测试】jmeter+Grafana+influxdb部署实战

URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
随机推荐
Three traversal methods of binary tree
[Jianzhi offer] 62 The last remaining number in the circle
[Jianzhi offer] 61 Shunzi in playing cards
兰空图床苹果快捷指令
激动人心!2022开放原子全球开源峰会报名火热开启!
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
C language to get program running time
BigDecimal除法的精度问题
Matery主题自定义(一)黑夜模式
The third lesson of EasyX learning
How MySQL uses JSON_ Extract() takes JSON value
Debug kernel code through proc interface
C # realizes crystal report binding data and printing 3-qr code barcode
Embedded-c Language-2
菜刀,蚁剑,冰蝎,哥斯拉的流量特征
Judge whether a number is a prime number (prime number)
Embedded -arm (bare board development) -1
CMake教程Step6(添加自定义命令和生成文件)