Skip to content

Commit

Permalink
fix: fix issues/1
Browse files Browse the repository at this point in the history
url: #1

Signed-off-by: Eglinux <xchaojun_525@sina.com>
  • Loading branch information
eglinuxer committed Aug 3, 2023
1 parent 8be7629 commit df81b33
Showing 1 changed file with 90 additions and 2 deletions.
92 changes: 90 additions & 2 deletions docs/cmake/basic/03_adding_usage_requirements_for_a_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,88 @@ cmake --build build

!!! tip "小提示"

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

## 练习 2 - 利用接口库制定 C++ 标准

既然我们已经将代码转换为更现代的方法,那么让我们来演示一下为多个目标设置属性的现代技术。

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

### 要完成的目标
添加 INTERFACE 库目标,指定所需的 C++ 标准。

### 参考资源
- [add_library()]
- [target_compile_features()]
- [target_link_libraries()]

### 需要编辑的文件
- `CMakeLists.txt`
- `MathFunctions/CMakeLists.txt`

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

[第 3 步练习 1](./03_adding_usage_requirements_for_a_library.md#1-)结束时留下的内容开始本练习。您必须完成 `TODO 4``TODO 7`

首先编辑顶层 `CMakeLists.txt` 文件。构建名为 `tutorial_compiler_flags``INTERFACE` 库目标,并指定 `cxx_std_11` 为目标编译器特征。

修改 `CMakeLists.txt``MathFunctions/CMakeLists.txt`,使所有目标都有 [target_link_libraries()] 调用 `tutorial_compiler_flags`

### 构建和运行
由于我们已经在[练习 1](./03_adding_usage_requirements_for_a_library.md#1-) 中配置了构建目录,因此只需调用以下命令重建代码即可:

``` shell
cd build
cmake --build .
```

接下来,使用新构建的 `Tutorial`,并验证其是否按预期运行。

### 解决方案
让我们更新上一步的代码,使用接口库来设置 `C++` 要求。

首先,我们需要删除变量 [CMAKE_CXX_STANDARD][CMAKE_CXX_STANDARD_REQUIRED] 上的两个 [set()] 调用。需要删除的具体行列如下:

``` cmake title="CMakeLists.txt"
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
```

接下来,我们需要创建一个接口库 `tutorial_compiler_flags`。然后使用 [target_compile_features()] 添加编译器特性 `cxx_std_11`

??? example "点击展开查看 `TODO 4`"

``` cmake title="CMakeLists.txt"
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
```

最后,设置好界面库后,我们需要将可执行目标、`MathFunctions` 库和 `SqrtLibrary` 库链接到新的 `tutorial_compiler_flags` 库。代码将分别如下:

??? example "点击展开查看 `TODO 5`"

``` cmake title="CMakeLists.txt"
target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
```
还有

??? example "点击展开查看 `TODO 6`"

``` cmake title="CMakeLists.txt"
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
```

以及

??? example "点击展开查看 `TODO 7`"

``` cmake title="CMakeLists.txt"
target_link_libraries(MathFunctions PUBLIC SqrtLibrary)
```

这样,我们的所有代码仍然需要 `C++ 11` 才能构建。不过请注意,有了这种方法,我们就能明确哪些目标需要特定的要求。此外,我们还在接口库中创建了一个单一的真相源。

[target_compile_definitions()]: https://cmake.org/cmake/help/latest/command/target_compile_definitions.html#command:target_compile_definitions
[target_compile_options()]: https://cmake.org/cmake/help/latest/command/target_compile_options.html#command:target_compile_options
Expand All @@ -103,4 +184,11 @@ cmake --build build
[CMAKE_CURRENT_SOURCE_DIR]: https://cmake.org/cmake/help/latest/variable/CMAKE_CURRENT_SOURCE_DIR.html#variable:CMAKE_CURRENT_SOURCE_DIR
[cmake]: https://cmake.org/cmake/help/latest/manual/cmake.1.html#manual:cmake(1)
[cmake-gui]: https://cmake.org/cmake/help/latest/manual/cmake-gui.1.html#manual:cmake-gui(1)
[ccmake]: https://cmake.org/cmake/help/latest/manual/ccmake.1.html#manual:ccmake(1)
[ccmake]: https://cmake.org/cmake/help/latest/manual/ccmake.1.html#manual:ccmake(1)
[生成器表达式]: https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#manual:cmake-generator-expressions(7)
[add_library()]: https://cmake.org/cmake/help/latest/command/add_library.html#command:add_library
[target_compile_features()]: https://cmake.org/cmake/help/latest/command/target_compile_features.html#command:target_compile_features
[target_link_libraries()]: https://cmake.org/cmake/help/latest/command/target_link_libraries.html#command:target_link_libraries
[CMAKE_CXX_STANDARD]: https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html#variable:CMAKE_CXX_STANDARD
[CMAKE_CXX_STANDARD_REQUIRED]: https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD_REQUIRED.html#variable:CMAKE_CXX_STANDARD_REQUIRED
[set()]: https://cmake.org/cmake/help/latest/command/set.html#command:set

0 comments on commit df81b33

Please sign in to comment.