8.MariaDB笔记——cmake使用介绍三关于库

8.MariaDB笔记——cmake使用介绍三关于库

继续,增加一个库。

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

#include

#include

#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

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中,不用每次都进行设置

然后在顶级目录MathFunctionsCMakeLists文件中最后几行设置如下:

# 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

#include

#include

#include "TutorialConfig.h"

#ifdef USE_MYMATH

                  #include

#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

 

 

你可能感兴趣的:(MariaDB)