CMake 工具使用教程系列(二)-Step2 -创建并引用静态库

本示例文件见上一篇博文,英文原文见https://cmake.org/cmake/help/v3.17/guide/tutorial/index.html#a-basic-starting-point-step-1
现在,我们将库添加到我们的项目中。 该库将包含我们自己的实现,用于计算数字的平方根。 然后可执行文件可以使用此库而不是编译器提供的标准平方根函数。

在本教程中,我们将库放入名为MathFunctions的子目录中。 该目录已包含头文件MathFunctions.h和源文件mysqrt.cxx。 源文件具有一个称为mysqrt的函数,该函数提供与编译器的sqrt函数类似的功能。

1 添加add_library命令(在MathFunctions文件夹下的CMakeLists.txt文件中)

将以下一行CMakeLists.txt文件添加到MathFunctions目录:

add_library(MathFunctions mysqrt.cxx)

2 利用add_subdirectory命令添加子目录到项目中

为了使用新库,我们将在顶层CMakeLists.txt文件中添加add_subdirectory()以便构建该和调用该库。 我们将新库添加到可执行文件,并将MathFunctions添加为包含目录,以便可以找到mqsqrt.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" #添加头文件目录
                          )

3 将使用库设为可选项

现在让我们将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时都设置该值。在dos命令行中打开或者关闭该库的使用如下"-DUSE_MYMATH=ON"或者"-DUSE_MUMATH=OFF"即可

4 编写if语句

下一个更改是使建立和链接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_INCLUDES类似地用于可选的头文件。 在处理许多可选组件时,这是一种经典方法,我们将在下一步中介绍现代方法。

5 对源代码的更改

对源代码的相应更改非常简单。 首先,如果需要,请在tutorial.cxx中包含MathFunctions.h头文件:

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

然后,在同一文件中,使USE_MYMATH控制使用哪个平方根函数:

#ifdef USE_MYMATH
  const double outputValue = mysqrt(inputValue);
#else
  const double outputValue = sqrt(inputValue);
#endif

由于源代码现在需要USE_MYMATH,因此我们可以使用以下行将其添加到TutorialConfig.h.in中:

#cmakedefine USE_MYMATH

6配置

请注意,在dos下执行命令时,可以利用-DCMAKE_BUILD_TYPE=Debug -DUSE_MYMATH=ON命令来控制使用库,如图1所示,那么,配置完成的CMakeCache.txt文件中的结果图2所示。

图1 cmake指令

图2 USE_MYMATH打开

7 创建

创建命令执行结果如下:


图 3 build结果

8 测试

图 4 测试结果

利用了迭代求解,说明调用库成功。

--青春未腐,光阴还长!--2020.05.04

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。