• CMake Tutorial 巡礼(3)_添加库的使用需求


    CMake Tutorial巡礼(3)_ 添加库的使用需求

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

    本章导读

    在这里插入图片描述

    第三步: 添加库的使用需求

    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内目标的可传递属性。利用“使用需求”的主要命令有以下几个:

    • target_compile_definitions()
    • target_compile_options()
    • target_include_directories()
    • target_link_libraries()

    Let’s refactor our code from Adding a Library to use the modern CMake approach of usage requirements. We first state that anybody linking to MathFunctions needs to include the current source directory, while MathFunctions itself doesn’t. So this can become an INTERFACE usage requirement.

    让我们将代码从上一章的“添加库”重构为使用现代CMake方法处理使用需求。我们首先声明,任何链接到MathFunctions的人都需要包含当前源目录,而MathFunctions本身则不需要。因此,这可以成为“接口”的使用需求。

    Remember INTERFACE means things that consumers require but the producer doesn’t. Add the following lines to the end of MathFunctions/CMakeLists.txt:

    请记住,“接口”表明消费者需要而生产者不需要。在MathFunctions/CMakeLists.txt的末尾添加以下几行:

    MathFunctions/CMakeLists.txt

    target_include_directories(MathFunctions
              INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
              )
    
    • 1
    • 2
    • 3

    小白按:MathFunctions/CMakeLists.txt修改完成后的版本为

    add_library(MathFunctions mysqrt.cxx)
    
    target_include_directories(MathFunctions
    	INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
    	)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Now that we’ve specified usage requirements for MathFunctions we can safely remove our uses of the EXTRA_INCLUDES variable from the top-level CMakeLists.txt, here:

    既然我们已经指定了MathFunctions的使用需求,我们就可以安全地从顶层的CMakeLists.txt中删除对EXTRA_INCLUDES变量的使用了,此处:

    CMakeLists.txt

    if(USE_MYMATH)
      add_subdirectory(MathFunctions)
      list(APPEND EXTRA_LIBS MathFunctions)
    endif()
    
    • 1
    • 2
    • 3
    • 4

    And here:

    还有这里的:

    CMakeLists.txt

    target_include_directories(Tutorial PUBLIC
                               "${PROJECT_BINARY_DIR}"
                               )
    
    • 1
    • 2
    • 3

    小白按:对比源文件可以看出,这个地方实际上是删除了

    list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
    
    • 1

    以及

    target_include_directories(Tutorial PUBLIC
                               "${PROJECT_BINARY_DIR}"
                               ${EXTRA_INCLUDES}
                               )
    
    • 1
    • 2
    • 3
    • 4

    中的${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}"
                               )
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    Once this is done, run the cmake executable or the cmake-gui to configure the project and then build it with your chosen build tool or by using cmake --build . from the build directory.

    完成后,运行 cmake 可执行文件或 cmake-gui来配置项目,然后使用您选择的构建工具或使用cmake --build .来从编译目录中构建它。

    小白按:这章内容很短,但其实它很重要。这一章贯穿了“接口”的思想,正是因为有了这样的设计,才简化了库组合时的操作。当你使用一个新的库时,不必再像上一节所说的那样在顶层CMakeLists.txt里面做过多的设计。

    【水平所限,错漏难免,创作不易,轻喷勿骂】

    在这里插入图片描述

  • 相关阅读:
    牛客网刷题【BC33、BC56、BC44、BC91、BC49、写函数求最大值】
    SpringMVC项目整合SSM统一结果封装
    k8s卷管理-2
    rpc报错com.netflix.hystrix.exception.HystrixRuntimeException
    第三次CCF计算机软件能力认证
    React项目中使用Echarts
    分享一些常用的小程序免费源码
    pytorch深度学习实战lesson13
    继电器在信号系统中的应用
    请编写一个函数void fun(char*ss),其功能是:将字符串ss中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。
  • 原文地址:https://blog.csdn.net/horsee/article/details/126775409