这是本系列的第四篇。
上一篇,我们学习了如何给项目添加一个库。接下来我们学习一下如何为添加的库添加使用需求。

Usage requirements allow for far better control over a library or executable’s link and include line while also giving more control over the transitive property of targets inside CMake. The primary commands that leverage usage requirements are:
“使用需求”允许更好地控制库或可执行文件的链接和包含行,同时还可以更好地控制CMake内目标的可传递属性。利用“使用需求”的主要命令有以下几个:
Let’s refactor our code from
Adding a Libraryto use the modern CMake approach of usage requirements. We first state that anybody linking toMathFunctionsneeds to include the current source directory, whileMathFunctionsitself doesn’t. So this can become anINTERFACEusage requirement.
让我们将代码从上一章的“添加库”重构为使用现代CMake方法处理使用需求。我们首先声明,任何链接到MathFunctions的人都需要包含当前源目录,而MathFunctions本身则不需要。因此,这可以成为“接口”的使用需求。
Remember
INTERFACEmeans things that consumers require but the producer doesn’t. Add the following lines to the end ofMathFunctions/CMakeLists.txt:
请记住,“接口”表明消费者需要而生产者不需要。在MathFunctions/CMakeLists.txt的末尾添加以下几行:
MathFunctions/CMakeLists.txt
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
小白按:MathFunctions/CMakeLists.txt修改完成后的版本为
add_library(MathFunctions mysqrt.cxx)
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
Now that we’ve specified usage requirements for
MathFunctionswe can safely remove our uses of theEXTRA_INCLUDESvariable from the top-levelCMakeLists.txt, here:
既然我们已经指定了MathFunctions的使用需求,我们就可以安全地从顶层的CMakeLists.txt中删除对EXTRA_INCLUDES变量的使用了,此处:
CMakeLists.txt
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
And here:
还有这里的:
CMakeLists.txt
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
小白按:对比源文件可以看出,这个地方实际上是删除了
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
以及
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
${EXTRA_INCLUDES}
)
中的${EXTRA_INCLUDES}
小白按:CMakeLists.txt修改完成后的版本为
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Tutorial VERSION 1.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# should we use our own math functions
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)
# add the MathFunctions library
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS 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}"
)
Once this is done, run the
cmakeexecutable or thecmake-guito configure the project and then build it with your chosen build tool or by usingcmake --build .from the build directory.
完成后,运行 cmake 可执行文件或 cmake-gui来配置项目,然后使用您选择的构建工具或使用cmake --build .来从编译目录中构建它。
小白按:这章内容很短,但其实它很重要。这一章贯穿了“接口”的思想,正是因为有了这样的设计,才简化了库组合时的操作。当你使用一个新的库时,不必再像上一节所说的那样在顶层CMakeLists.txt里面做过多的设计。
【水平所限,错漏难免,创作不易,轻喷勿骂】
