2 增加一个库
现在我们将向项目中添加一个库。这个库将包含我们自己的实现,用于计算数字的平方根。然后可执行文件可以使用这个库,而不是编译器提供的标准平方根函数。
在本教程中,我们将把库放入一个名为MathFunctions的子目录中。此目录已包含头文件MathFunctions.h和源文件mysqrt.cxx。源文件有一个名为mysqrt的函数,它提供了与编译器的sqrt函数类似的功能。
将以下单行CMakeLists.txt文件添加到MathFunctions目录:
add_library(MathFunctions mysqrt.cxx)
为了使用新的库,我们将在顶级CMakeLists.txt文件中添加一个add\u subdirectory()调用,以便构建库。我们将新库添加到可执行文件中,并将MathFunctions添加为include目录,以便可以找到mysqrt.h头文件。顶级CMakeLists.txt文件的最后几行现在应该如下所示:
# 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"
)
现在让我们使MathFunctions库成为可选的。虽然对于教程来说真的不需要这样做,但是对于大型项目来说这是很常见的。第一步是向顶级CMakeLists.txt文件添加一个选项。
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)
此选项将显示在cmake gui和ccmake中,默认值为ON,用户可以更改。此设置将存储在缓存中,这样用户就不需要在每次对生成目录运行CMake时设置该值。
下一个变化是使MathFunctions库的构建和链接成为有条件的。为此,我们将顶级CMakeLists.txt文件的结尾更改为如下所示:
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}
)
注意使用变量EXTRA_LIBS收集任何可选库,以便稍后链接到可执行文件。变量EXTRA\u INCLUDES类似地用于可选头文件。这是处理许多可选组件时的经典方法,我们将在下一步介绍现代方法。
对源代码的相应更改相当简单。首先,在tutorial.cxx中,如果需要,请包含MathFunctions.h头:
#ifdef USE_MYMATH
# include "MathFunctions.h"
#endif
然后,在同一个文件中,使用\u MYMATH控件使用哪个平方根函数:
#ifdef USE_MYMATH
const double outputValue = mysqrt(inputValue);
#else
const double outputValue = sqrt(inputValue);
#endif
由于源代码现在要求使用\u MYMATH,我们可以使用以下行将其添加到TutorialConfig.h.in:
#cmakedefine USE_MYMATH
练习:为什么在使用\u MYMATH选项之后配置TutorialConfig.h.in很重要?如果我们把两者颠倒过来会发生什么?
运行cmake可执行文件或cmakegui来配置项目,然后使用您选择的构建工具构建它。然后运行构建的教程可执行文件。
现在让我们更新USE\u MYMATH的值。最简单的方法是在终端中使用cmakegui或ccmake。或者,如果要从命令行更改选项,请尝试:
cmake ../Step2 -DUSE_MYMATH=OFF
重新生成并再次运行教程。
哪个函数的结果更好,sqrt还是mysqrt?