CMake官方教程中文翻译 Step 3: Adding Usage Requirements for a Library

鉴于自己破烂的英语,所以把cmake的官方文档用 谷歌翻译 翻译下来方便查看。
英语好的同学建议直接去看cmake官方文档(英文)学习:地址 点这里
或复制:https://cmake.org/cmake/help/latest/guide/tutorial/index.html

因为官方文档有点多,所以只截取CMake Tutorial的 step1——step12 进行翻译,这是第3步的翻译,以下是每一步的翻译连接:
Documentation » CMake Tutorial » Step 1: A Basic Starting Point
Documentation » CMake Tutorial » Step 2: Adding a Library
[Documentation » CMake Tutorial » Step 3: Adding Usage Requirements for a Library]
Documentation » CMake Tutorial » Step 4: Adding Generator Expressions
Documentation » CMake Tutorial » Step 5: Installing and Testing
Documentation » CMake Tutorial » Step 6: Adding Support for a Testing Dashboard
Documentation » CMake Tutorial » Step 7: Adding System Introspection
Documentation » CMake Tutorial » Step 8: Adding a Custom Command and Generated File
Documentation » CMake Tutorial » Step 9: Packaging an Installer
Documentation » CMake Tutorial » Step 10: Selecting Static or Shared Libraries
Documentation » CMake Tutorial » Step 11: Adding Export Configuration
Documentation » CMake Tutorial » Step 12: Packaging Debug and Release

谷歌翻译可能有错,此文档的目的仅是加快观看英文官方文档的速度,所以请结合英文官方文档观看

Step 3: Adding Usage Requirements for a Library

Exercise 1 - Adding Usage Requirements for a Library

目标参数的使用要求允许更好地控制库或可执行文件的链接和包含行,同时还可以更好地控制 CMake 内目标的传递属性。 利用使用要求的主要命令是:
target_compile_definitions()
target_compile_options()
target_include_directories()
target_link_directories()
target_link_options()
target_precompile_headers()
target_sources()

Goal

添加库的使用要求。

Helpful Materials

CMAKE_CURRENT_SOURCE_DIR

Files to Edit

MathFunctions/CMakeLists.txt
CMakeLists.txt

Getting Started

在本练习中,我们将重构添加库中的代码以使用现代 CMake 方法。 我们将让我们的库定义自己的使用要求,以便根据需要将它们传递到其他目标。 在这种情况下,MathFunctions 将指定任何所需的包含目录本身。 然后,使用目标教程只需要链接到 MathFunctions,而不用担心任何其他包含目录。

Step3 目录中提供了起始源代码。 在此练习中,完成 TODO 1 到 TODO 3。

首先,在 MathFunctions/CMakeLists 中添加对 target_include_directories() 的调用。 请记住,CMAKE_CURRENT_SOURCE_DIR 是当前正在处理的源目录的路径。

然后,更新(并简化!)顶级 CMakeLists.txt 中对 target_include_directories() 的调用。

Build and Run

创建一个名为 Step3_build 的新目录,运行 cmake 可执行文件或 cmake-gui 来配置项目,然后使用您选择的构建工具或使用 cmake --build 来构建它。 从构建目录。 下面是命令行的回顾:
mkdir Step3_build
cd Step3_build
cmake …/Step3
cmake --build .

接下来,使用新建的教程并验证它是否按预期工作。

Solution

让我们更新上一步中的代码以使用现代的 CMake 方法来满足使用要求。

我们想要声明的是,任何链接到 MathFunctions 的人都需要包含当前的源目录,而 MathFunctions 本身则不需要。 这可以用 INTERFACE 使用要求来表达。 请记住,接口意味着消费者需要但生产者不需要的东西。

在 MathFunctions/CMakeLists.txt 的末尾,将 target_include_directories() 与 INTERFACE 关键字一起使用,如下所示:
TODO 1: MathFunctions/CMakeLists.txt
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)

现在我们已经指定了 MathFunctions 的使用要求,我们可以安全地从顶级 CMakeLists.txt 中删除对 EXTRA_INCLUDES 变量的使用。

删除这一行:
TODO 2: CMakeLists.txt
list(APPEND EXTRA_INCLUDES “${PROJECT_SOURCE_DIR}/MathFunctions”)

并从 target_include_directories 中删除 EXTRA_INCLUDES:
删除这一行:
TODO 3: CMakeLists.txt
target_include_directories(Tutorial PUBLIC
“${PROJECT_BINARY_DIR}”
)

请注意,使用这种技术,我们的可执行目标使用我们的库所做的唯一事情就是使用库目标的名称调用 target_link_libraries() 。 在较大的项目中,手动指定库依赖项的经典方法很快就会变得非常复杂。

Exercise 2 - Setting the C++ Standard with Interface Libraries

现在我们已经将代码切换为更现代的方法,让我们演示一种为多个目标设置属性的现代技术。

让我们重构现有代码以使用 INTERFACE 库。 我们将在下一步中使用该库来演示生成器表达式的常见用途。

Goal

添加 INTERFACE 库目标以指定所需的 C++ 标准。

Helpful Resources

add_library()
target_compile_features()
target_link_libraries()

Files to Edit

CMakeLists.txt
MathFunctions/CMakeLists.txt

Getting Started

在本练习中,我们将重构代码以使用 INTERFACE 库来指定 C++ 标准。

从我们在步骤 3 练习 1 结束时留下的内容开始此练习。您必须完成 TODO 4 到 TODO 7。

首先编辑顶级 CMakeLists.txt 文件。 构造一个名为tutorial_compiler_flags 的 INTERFACE 库目标,并将 cxx_std_11 指定为目标编译器功能。

修改 CMakeLists.txt 和 MathFunctions/CMakeLists.txt ,以便所有目标都有对tutorial_compiler_flags 的 target_link_libraries() 调用。

Build and Run

由于我们已经在练习 1 中配置了构建目录,因此只需通过调用以下命令来重建我们的代码:
cd Step3_build
cmake --build .

接下来,使用新建的教程并验证它是否按预期工作。

Solution

让我们更新上一步的代码以使用接口库来设置 C++ 要求。

首先,我们需要删除对变量 CMAKE_CXX_STANDARD 和 CMAKE_CXX_STANDARD_REQUIRED 的两个 set() 调用。 具体删除的行如下:
CMakeLists.txt
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True

接下来,我们需要创建一个界面库,tutorial_compiler_flags。 然后使用 target_compile_features() 添加编译器功能 cxx_std_11。
TODO 4: CMakeLists.txt
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)

最后,设置好界面库后,我们需要将可执行的Tutorial、SqrtLibrary 库和MathFunctions 库链接到新的tutorial_compiler_flags 库。 分别,代码将如下所示:
TODO 5: CMakeLists.txt
target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)

这个:
TODO 6: MathFunctions/CMakeLists.txt
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)

和这个:
TODO 7: MathFunctions/CMakeLists.txt
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)

这样,我们所有的代码仍然需要 C++ 11 来构建。 但请注意,使用此方法,它使我们能够具体了解哪些目标具有特定要求。 此外,我们在界面库中创建了单一事实来源。

  • 30
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值