8.MariaDB笔记——cmake使用介绍三关于库
2017-10-11 22:44
381 查看
8.MariaDB笔记——cmake使用介绍三关于库
继续,增加一个库。
增加一个子目录MathFunctions.子目录增加一个CMakeList文件,内容只有一行如下:
add_library(MathFunctionsmysqrt.cxx)
其中mysqrt.cxx中包含一个函数叫做mysqrt,提供开方的功能。
Add_library表明是新库。
mysqrt
为使用新的库,在主的CMakeList文件中.增加如下:
最后一行修改如下
include_directories("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory(MathFunctions)
# add theexecutable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial MathFunctions)
include_directories可以找到子目录中的头文件的函数原型
add_subdirectory表明需要增加新库,使得子库可以得到编译
target_link_libraries 增加目标连接
#include <stdio.h>
#include <stdlib.h>
#include <mysqrt.h>
#include "TutorialConfig.h"
int main (int argc, char *argv[])
{
if (argc <2)
{
fprintf(stdout,"%s Version %d.%d\n",
argv[0],
Tutorial_VERSION_MAJOR,
Tutorial_VERSION_MINOR);
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
doubleinputValue = atof(argv[1]);
doubleoutputValue = mysqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
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);//精度控制
returnmid;
}
cmake –build .
然后测试如下:
F:\cmake_zhizuo\Debug>Tutorial.exe 4
The square root of 4 is 1.99726
这个在第三方代码提供库就有用了。
首先要增加一个可选项在顶级目录的CMakeLists文件中,
如下:
option(USE_MYMATH "Use tutorial provided math implementation" ON)
可以通过CMake GUI来设置该默认值,ON表示可以被用户进行改变。
或者在TutorialConfig.h文件中加入如下行:
#cmakedefineUSE_MYMATH
表示启动USE_MYMATH
该值会存在cache中,不用每次都进行设置
然后在顶级目录MathFunctions的CMakeLists文件中最后几行设置如下:
# 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})
使用USE_MYMATH来确定是否使用MathFunctions。
变量EXTRA_LIBS表示可选的库连接到可执行文件。
这个在大项目中经常用于保持多个选项组建干净。
project (Tutorial)
option (USE_MYMATH "Use tutorial provided mathimplementation" ON)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
# configure a header file to pass some of the CMakesettings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# add the binary tree to the search path for includefiles
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add the executable
# add the MathFunctions library?
#
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
set(EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial ${EXTRA_LIBS})
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
#ifdef USE_MYMATH
#include<mysqrt.h>
#endif
int main (intargc, char *argv[])
{
if (argc <2)
{
fprintf(stdout,"%s Version %d.%d\n", argv[0],
Tutorial_VERSION_MAJOR,
Tutorial_VERSION_MINOR);
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
doubleinputValue = atof(argv[1]);
#ifdefUSE_MYMATH
doubleoutputValue = mysqrt(inputValue);
#else
doubleoutputValue = sqrt(inputValue);
#endif
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
#defineUSE_MYMATH
启用USE_MYMATH
测试如下:
启用和不启用后的对比:
F: \cmake_zhizuo\Debug>Tutorial.exe 4
The square root of 4 is 2
F:\ cmake_zhizuo\Debug>Tutorial.exe 4
The square root of 4 is 2.00781
继续,增加一个库。
cmake如何增加库
增加一个库到项目中.增加一个子目录MathFunctions.子目录增加一个CMakeList文件,内容只有一行如下:
add_library(MathFunctionsmysqrt.cxx)
其中mysqrt.cxx中包含一个函数叫做mysqrt,提供开方的功能。
Add_library表明是新库。
mysqrt
为使用新的库,在主的CMakeList文件中.增加如下:
最后一行修改如下
include_directories("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory(MathFunctions)
# add theexecutable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial MathFunctions)
include_directories可以找到子目录中的头文件的函数原型
add_subdirectory表明需要增加新库,使得子库可以得到编译
target_link_libraries 增加目标连接
tutorial.cxx修改如下
// A simple program that computes the square root ofa number#include <stdio.h>
#include <stdlib.h>
#include <mysqrt.h>
#include "TutorialConfig.h"
int main (int argc, char *argv[])
{
if (argc <2)
{
fprintf(stdout,"%s Version %d.%d\n",
argv[0],
Tutorial_VERSION_MAJOR,
Tutorial_VERSION_MINOR);
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
doubleinputValue = atof(argv[1]);
doubleoutputValue = mysqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
mysqrt.h
double mysqrt(double n) ;mysqrt.cxx
#include <math.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);//精度控制
returnmid;
}
执行
cmake .cmake –build .
然后测试如下:
F:\cmake_zhizuo\Debug>Tutorial.exe 4
The square root of 4 is 1.99726
可选库
如果让MathFunctions库变成可选的。这个在第三方代码提供库就有用了。
首先要增加一个可选项在顶级目录的CMakeLists文件中,
如下:
option(USE_MYMATH "Use tutorial provided math implementation" ON)
可以通过CMake GUI来设置该默认值,ON表示可以被用户进行改变。
或者在TutorialConfig.h文件中加入如下行:
#cmakedefineUSE_MYMATH
表示启动USE_MYMATH
该值会存在cache中,不用每次都进行设置
然后在顶级目录MathFunctions的CMakeLists文件中最后几行设置如下:
# 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})
使用USE_MYMATH来确定是否使用MathFunctions。
变量EXTRA_LIBS表示可选的库连接到可执行文件。
这个在大项目中经常用于保持多个选项组建干净。
顶层CMakeLists
cmake_minimum_required (VERSION 2.6)project (Tutorial)
option (USE_MYMATH "Use tutorial provided mathimplementation" ON)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
# configure a header file to pass some of the CMakesettings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# add the binary tree to the search path for includefiles
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add the executable
# add the MathFunctions library?
#
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
set(EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial ${EXTRA_LIBS})
主tutorial.cxx
#include <stdio.h>#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
#ifdef USE_MYMATH
#include<mysqrt.h>
#endif
int main (intargc, char *argv[])
{
if (argc <2)
{
fprintf(stdout,"%s Version %d.%d\n", argv[0],
Tutorial_VERSION_MAJOR,
Tutorial_VERSION_MINOR);
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
doubleinputValue = atof(argv[1]);
#ifdefUSE_MYMATH
doubleoutputValue = mysqrt(inputValue);
#else
doubleoutputValue = sqrt(inputValue);
#endif
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
测试
在TutorialConfig.h文件中加入如下行:#defineUSE_MYMATH
启用USE_MYMATH
测试如下:
启用和不启用后的对比:
F: \cmake_zhizuo\Debug>Tutorial.exe 4
The square root of 4 is 2
F:\ cmake_zhizuo\Debug>Tutorial.exe 4
The square root of 4 is 2.00781
相关文章推荐
- 10.MariaDB笔记——cmake使用介绍五系统自带函数
- 9.MariaDB笔记——cmake使用介绍四安装与测试
- 安卓开发笔记——关于开源项目SlidingMenu的使用介绍(仿QQ5.0侧滑菜单)
- 11.MariaDB笔记——cmake使用介绍六动态产生代码文件
- 13.MariaDB笔记——cmake使用介绍八支持面板
- 7.MariaDB笔记——cmake使用介绍二
- 12.MariaDB笔记——cmake使用介绍七编译安装包
- 6.MariaDB笔记——cmake使用介绍一
- 关于使用LoadRunner对Appeon Web应用进行压力测试的初步介绍
- 关于使用LoadRunner对Appeon Web应用进行压力测试的初步介绍
- 介绍关于IBM MQSeries的使用指南
- 关于Linux Grep命令使用的详细介绍
- [翻译练习]关于在传统ASP.NET Web应用程序上面使用Routing的一些笔记
- 关于GTK各个循环函数的介绍与使用
- 09.3.3日 mfc 笔记 关于CBitmapButton的使用方法 和 playsound()函数
- 关于JSON的介绍、使用和疑问
- [转]prototype.js开发笔记(针对Ajax的JS框架Prototype的使用介绍)
- NUnit学习笔记3--Attribute使用介绍
- 关于oracle日期函数的介绍和使用
- .NET控件关于Popup Win控件的使用介绍