11.MariaDB笔记——cmake使用介绍六动态产生代码文件
2017-10-11 22:45
411 查看
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;
}
如下:
# 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文件如下:
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")
#defineTutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefineUSE_MYMATH
//does the platform provide exp and log functions?
#cmakedefineHAVE_LOG
#cmakedefineHAVE_EXP
#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
都是返回同一个值,因为函数写死了。
实现了动态产生代码并加入到程序中了。
继续学习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
都是返回同一个值,因为函数写死了。
实现了动态产生代码并加入到程序中了。
相关文章推荐
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 详细介绍附代码:使用jquery,和php文件构建一个简单的在线聊天室,通过ip显示googlemap
- [代码笔记] python 之网络编程: 使用udp 发送音频文件
- CEF3自研究笔记 一、使用CMake创建CEF3的vs2013的工程文件
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 安卓逆向学习笔记 (4) - 使用IDA Pro动态调试so文件
- 【学习笔记二】struts2的动态下载,使用Apache PoI生成excel表格,将用户信息以xls文件格式供用户下载
- asp.net cshtml页面使用Razor后台代码动态产生页面——函数实现
- 9.MariaDB笔记——cmake使用介绍四安装与测试
- 代码动态模板的使用和配置文件下载
- 10.MariaDB笔记——cmake使用介绍五系统自带函数
- Android中不使用布局文件,java代码动态创建布局及控件
- 6.MariaDB笔记——cmake使用介绍一
- 【代码笔记】iOS-json文件的使用
- WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
- 7.MariaDB笔记——cmake使用介绍二
- C#中使用代码动态改变配置文件信息
- 六、入侵Activiti-SpringBoot使用代码动态编辑流程文件
- <MFC笔记> VS2013动态库文件的创建及其使用详解
- 使用Ionic.Zip.dll压缩和解压文件代码笔记