CMake官方教程中文翻译 Step 7: Adding System Introspection

本文介绍了CMake教程的第7步,涉及添加系统introspection代码,检测log和exp函数的平台依赖性。通过Exercise1,学习者将实践使用CheckCXXSourceCompiles和target_compile_definitions来适应目标平台的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

因为官方文档有点多,所以只截取CMake Tutorial的 step1——step12 进行翻译,这是第7步的翻译,以下是每一步的翻译链接:
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 7: Adding System Introspection

让我们考虑向我们的项目添加一些代码,这些代码取决于目标平台可能没有的功能。 对于这个例子,我们将添加一些代码,具体取决于目标平台是否具有 log 和 exp 函数。 当然,几乎每个平台都有这些功能,但在本教程中假设它们并不常见。

Exercise 1 - Assessing Dependency Availability

Goal

根据可用的系统依赖项更改实现。

Helpful Resources

CheckCXXSourceCompiles
target_compile_definitions()

Files to Edit

MathFunctions/CMakeLists.txt
MathFunctions/mysqrt.cxx

Getting Started

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

首先编辑 MathFunctions/CMakeLists.txt。 包括 CheckCXXSourceCompiles 模块。 然后,使用 check_cxx_source_compiles 确定 log 和 exp 是否可从 cmath 中获得。 如果可用,请使用 target_compile_definitions() 将 HAVE_LOG 和 HAVE_EXP 指定为编译定义。

在 MathFunctions/mysqrt.cxx 中,包含 cmath。 然后,如果系统有 log 和 exp,则使用它们来计算平方根。

Build and Run

创建一个名为 Step7_build 的新目录。 运行 cmake 可执行文件或 cmake-gui 来配置项目,然后使用您选择的构建工具构建它并运行教程可执行文件。
这可能如下所示:

mkdir Step7_build
cd Step7_build
cmake …/Step7
cmake --build .

现在哪个函数可以提供更好的结果,sqrt 还是 mysqrt?

Solution

在本练习中,我们将使用 CheckCXXSourceCompiles 模块中的函数,因此首先我们必须将其包含在 MathFunctions/CMakeLists.txt 中。

TODO 1: MathFunctions/CMakeLists.txt
include(CheckCXXSourceCompiles)

然后使用 check_cxx_compiles_source 测试 log 和 exp 的可用性。 这个函数让我们在真正的源代码编译之前尝试编译具有所需依赖项的简单代码。 结果变量 HAVE_LOG 和 HAVE_EXP 表示这些依赖项是否可用。

TODO 2: MathFunctions/CMakeLists.txt
check_cxx_source_compiles("
#include
int main() {
std::log(1.0);
return 0;
}
" HAVE_LOG)
check_cxx_source_compiles("
#include
int main() {
std::exp(1.0);
return 0;
}
" HAVE_EXP)

接下来,我们需要将这些 CMake 变量传递给我们的源代码。 这样,我们的源代码就可以知道哪些资源可用。 如果 log 和 exp 都可用,请使用 target_compile_definitions() 将 HAVE_LOG 和 HAVE_EXP 指定为 PRIVATE 编译定义。

TODO 3: MathFunctions/CMakeLists.txt
if(HAVE_LOG AND HAVE_EXP)
target_compile_definitions(SqrtLibrary
PRIVATE “HAVE_LOG” “HAVE_EXP”
)
endif()

target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()

由于我们可能使用 log 和 exp,因此我们需要修改 mysqrt.cxx 以包含 cmath。

TODO 4: MathFunctions/mysqrt.cxx
#include

如果系统上有 log 和 exp 可用,则使用它们在 mysqrt 函数中计算平方根。 MathFunctions/mysqrt.cxx 中的 mysqrt 函数如下所示:

TODO 5: MathFunctions/mysqrt.cxx
#if defined(HAVE_LOG) && defined(HAVE_EXP)
double result = std::exp(std::log(x) * 0.5);
std::cout << “Computing sqrt of " << x << " to be " << result
<< " using log and exp” << std::endl;
#else
double result = x;

// do ten iterations
for (int i = 0; i < 10; ++i) {
if (result <= 0) {
result = 0.1;
}
double delta = x - (result * result);
result = result + 0.5 * delta / result;
std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
}
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值