cmake 官方入门说明 Step2 + ubuntu16.04一通执行过程[未完]

in 网站建设
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

Adding a Library (Step 2)

Now we will add a library to our project. This library will contain our own implementation for computing the square root of a number.
The executable can then use this library instead of the standard square root function provided by the compiler.
让可执行程序能使用这个库
For this tutorial we will put the library into a subdirectory calledMathFunctions.
在教程中,我们把这个库放到一个子目录文件MathFunctions中。
This directory already contains a header file,MathFunctions.h, and a source filemysqrt.cxx.
The source file has one function calledmysqrtthat provides similar functionality to the compiler’ssqrtfunction.

Add the following one lineCMakeLists.txtfile to theMathFunctionsdirectory:

add_library(MathFunctions  mysqrt.cxx)

To make use of the new library we will add anadd_subdirectorycall in the top-levelCMakeLists.txtfile so that the library will get built.
We add the new library to the executable, and addMathFunctionsas an include directory so that themqsqrt.hheader file can be found. The last few lines of the top-levelCMakeLists.txtfile should now look like:

# add the MathFunctions library
add_subdirectory(MathFunctions)

# add the executable
add_executable(Tutorial  tutorial.cxx)

target_link_libraries(Tutorial  PUBLIC  MathFunctions)

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial  PUBLIC
  "${PROJECT_BINARY_DIR}"
  "${PROJECT_SOURCE_DIR}MathFunctions"
  )

Now let us make the MathFunctions library optional. While for the tutorial there really isn’t any need to do so, for larger projects this is a common occurrence.
The first step is to add an option to the top-levelCMakeLists.txtfile.

option(USE_MYMATH  "Use tutorial provided math implementation"  ON)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in  TutorialConfig.h)

This option will be displayed in the CMake GUI and ccmake with a default value of ON that can be changed by the user. This setting will be stored in the cache so that the user does not need to set the value each time they run CMake on a build directory.

The next change is to make building and linking the MathFunctions library conditional. To do this we change the end of the top-levelCMakeLists.txtfile to look like the following:

if(USE_MYMATH)
  add_subdirectory(MathFunctions)
  list(APPEND  EXTRA_LIBS  MathFunctions)
  list(APPEND  EXTRA_INCLUDES  "${PROJECT_SOURCE_DIR}MathFunctions")
endif()

# add the executable
add_executable(Tutorial  tutorial.cxx)

target_link_libraries(Tutorial  PUBLIC  ${EXTRA_LIBS})

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial  PUBLIC
  "${PROJECT_BINARY_DIR}"
  ${EXTRA_INCLUDES}
  )

Note the use of the variableEXTRA_LIBSto collect up any optional libraries to later be linked into the executable. The variableEXTRA_INCLUDESis used similarly for optional header files. This is a classic approach when dealing with many optional components, we will cover the modern approach in the next step.

The corresponding changes to the source code are fairly straightforward. First, intutorial.cxx, include theMathFunctions.hheader if we need it:

#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif

Then, in the same file, makeUSE_MYMATHcontrol which square root function is used:

#ifdef USE\_MYMATH
  const double outputValue \= mysqrt(inputValue);
#else
  const double outputValue \= sqrt(inputValue);
#endif

Since the source code now requiresUSE_MYMATHwe can add it toTutorialConfig.h.inwith the following line:

#cmakedefine USE_MYMATH

Exercise: Why is it important that we configureTutorialConfig.h.inafter the option forUSE_MYMATH? What would happen if we inverted the two?

Runcmakeorcmake-guito configure the project and then build it with your chosen build tool. Then run the built Tutorial executable.

Use ccmake or the CMake GUI to update the value ofUSE_MYMATH. Rebuild and run the tutorial again. Which function gives better results, sqrt or mysqrt?

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看