11.MariaDB笔记——cmake使用介绍六动态产生代码文件
来源:互联网 发布:淘宝ar怎么用 编辑:程序博客网 时间:2024/05/20 03:07
11.MariaDB笔记——cmake使用介绍六动态产生代码文件
继续学习cmake.
怎样增加产生的代码文件到应用编译过程。
例如:我们在编译过程中创建一个提前计算好平方根的表,然后将产生的表编译到我们的应用中。
在MathFunctions子目录中创建MakeTable.cxx文件,内容如下:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
int i;
doubleresult;
// make surewe have enough arguments
if (argc <2)
{
return 1;
}
// open theoutput file
FILE *fout =fopen(argv[1],"w");
if (!fout)
{
return 1;
}
// create asource file with a table of square roots
fprintf(fout,"double sqrtTable[] = {\n");
for (i = 0; i< 10; ++i)
{
result =sqrt(static_cast<double>(i));
fprintf(fout,"%g,\n",result);
}
// close the tablewith a zero
fprintf(fout,"0};\n");
fclose(fout);
return 0;
}
子目录CMakeLists
接着增加合适的命令在MathFunctions的CMakeLists中来编译MakeTable可执行文件,然后作为编译过程。
如下:
# first we addthe executable that generates the table
add_executable(MakeTableMakeTable.cxx)
# add thecommand to generate the source code
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
COMMAND MakeTable${CMAKE_CURRENT_BINARY_DIR}/Table.h
DEPENDS MakeTable
)
# add thebinary tree directory to the search path for
# include files
include_directories(${CMAKE_CURRENT_BINARY_DIR} )
# add the mainlibrary
add_library(MathFunctionsmysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h )
使得CMake知道mysqrt.cxx依赖于产生的Table.h,把table.h加到MathFunctions库的源列表中。把当前二进制库目录加到include目录中,这样table.h可以被找到,并被mysqrt.cxx包含。当项目编译的时候先编译MakeTable可执行文件,然后运行MakeTable来产生table.h.
最后编译mysqrt.cxx来产生MathFunctions库文件。
为此,主CMakeLists文件如下:
主C MakeLists文件
cmake_minimum_required(VERSION 2.6)
project(Tutorial)
include(CTest)
# The versionnumber.
set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)
# does thissystem provide the log and exp functions?
include(${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
check_function_exists(log HAVE_LOG)
check_function_exists(exp HAVE_EXP)
# should weuse our own math functions
option(USE_MYMATH
"Use tutorial provided mathimplementation" ON)
# configure aheader file to pass some of the CMake settings
# to thesource code
configure_file(
"${PROJECT_SOURCE_DIR}/TutorialConfig.h"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# add thebinary tree to the search path for include files
# so that wewill find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add theMathFunctions library?
if(USE_MYMATH)
include_directories("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif(USE_MYMATH)
# add theexecutable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial ${EXTRA_LIBS})
# add theinstall targets
install(TARGETS Tutorial DESTINATION bin)
install (FILES"${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include)
# does theapplication run
add_test(TutorialRuns Tutorial 25)
# does theusage message work?
add_test(TutorialUsage Tutorial)
set_tests_properties(TutorialUsage
PROPERTIES
PASS_REGULAR_EXPRESSION"Usage:.*number"
)
#define amacro to simplify adding tests
macro (do_testarg result)
add_test (TutorialComp${arg} Tutorial ${arg})
set_tests_properties (TutorialComp${arg}
PROPERTIES PASS_REGULAR_EXPRESSION${result}
)
endmacro(do_test)
# do a bunchof result based tests
do_test (4"4 is 2")
do_test (9"9 is 3")
do_test (5"5 is 2.236")
do_test (7"7 is 2.645")
do_test (25"25 is 5")
do_test (-25"-25 is 0")
do_test(0.0001 "0.0001 is 0.01")
TutorialConfig.h文件如下
#defineTutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#defineTutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefineUSE_MYMATH
//does the platform provide exp and log functions?
#cmakedefineHAVE_LOG
#cmakedefineHAVE_EXP
子目录MathFunctions中mysqrt.cxx如下
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <Table.h>
double mysqrt(double n) //用二分法
{
if(n<0)//小于0的按照你需要的处理
returnn;
doublemid,last;
doublelow,up;
low=0,up=n;
mid=(low+up)/2;
do
{
if(mid*mid>n)
up=mid;
else
low=mid;
last=mid;
mid=(up+low)/2;
}while(abs(mid-last)> 0.01);//精度控制
returnsqrtTable[3];
}
此处为了测试,直接在最后一行返回了Table表中的值
return sqrtTable[3]
测试后如下:
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe6
The square root of 6 is 1.73205
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe6
The square root of 6 is 1.73205
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe3
The square root of 3 is 1.73205
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe2
The square root of 2 is 1.73205
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe1
The square root of 1 is 1.73205
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe5
The square root of 5 is 1.73205
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe6
The square root of 6 is 1.73205
都是返回同一个值,因为函数写死了。
实现了动态产生代码并加入到程序中了。
- 11.MariaDB笔记——cmake使用介绍六动态产生代码文件
- 6.MariaDB笔记——cmake使用介绍一
- 7.MariaDB笔记——cmake使用介绍二
- 8.MariaDB笔记——cmake使用介绍三关于库
- 9.MariaDB笔记——cmake使用介绍四安装与测试
- 10.MariaDB笔记——cmake使用介绍五系统自带函数
- 12.MariaDB笔记——cmake使用介绍七编译安装包
- 13.MariaDB笔记——cmake使用介绍八支持面板
- 5.MariaDB笔记——MariaDB开发者
- 六、入侵Activiti-SpringBoot使用代码动态编辑流程文件
- cmake 学习笔记(六)
- CMake学习笔记(4)——使用外部共享库和头文件
- mariadb使用笔记
- 2.MariaDB笔记——MariaDB源码编译
- 3.MariaDB笔记——MariaDB在Windows安装
- 4.MariaDB笔记——MariaDB在Windows源码编译
- cmake使用一 —— cmake语法
- cmake使用一 —— cmake语法
- 计算机系统的重装教程
- 字符串笔试题
- 10.MariaDB笔记——cmake使用介绍五系统自带函数
- VMware Workstation下安装Ubuntu 64位系统
- Android Dalvik VM内存优化之内存泄露篇。
- 11.MariaDB笔记——cmake使用介绍六动态产生代码文件
- C++ explicit关键字详解
- Linux-RAID详解及配置
- centos7mini版网络链接配置
- 12.MariaDB笔记——cmake使用介绍七编译安装包
- 分治法求最大公约数gcd
- Leetcode:Two Sum
- 160个练手CrackMe-008
- 实验一(模板类操作)