吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 528|回复: 4
收起左侧

[已解决] CMake官方教程里的一个疑问

 关闭 [复制链接]
aoyu321 发表于 2024-1-2 16:16
本帖最后由 aoyu321 于 2024-1-2 17:06 编辑

大家好,我正在看cmake的官方教程,看到Step2的时候有一个地方感觉很困惑。

链接:https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20a%20Library.html

在 练习2 的TODO13,CMakeLists.txt 是这样来写的:

[Shell] 纯文本查看 复制代码
# TODO 13: When USE_MYMATH is ON, link SqrtLibrary to the MathFunctions Library
# 这里我没看明白,因为下面这句代码不能体现出 USE_MYMATH 是否存在带来的选择性
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)


我很奇怪,当 USE_MYMATH 这个参数为 ON 时就把 SqrtLibrary 链接到 MathFunctions,可是我就是很纳闷,这样一种条件判断,是怎么通过这句代码表达出来的,按照我的认知,难道不应该要用一个 if 条件判断吗。

完整 CMakeLists.txt 如下:

[Shell] 纯文本查看 复制代码
# TODO 14: Remove mysqrt.cxx from the list of sources
add_library(MathFunctions MathFunctions.cxx)

# TODO 1: Add a library called MathFunctions with sources MathFunctions.cxx
# and mysqrt.cxx
# Hint: You will need the add_library command
# add_library(MathFunctions MathFunctions.cxx mysqrt.cxx)

# TODO 7: Create a variable USE_MYMATH using option and set default to ON
option(USE_MYMATH "Use tutorial provided math implementation" ON)

# TODO 8: If USE_MYMATH is ON, use target_compile_definitions to pass
# USE_MYMATH as a precompiled definition to our source files
if (USE_MYMATH)
    target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
endif()

# TODO 12: When USE_MYMATH is ON, add a library for SqrtLibrary with
# source mysqrt.cxx
add_library(SqrtLibrary STATIC mysqrt.cxx)

# TODO 13: When USE_MYMATH is ON, link SqrtLibrary to the MathFunctions Library
# 这里我没看明白,因为下面这句代码不能体现出 USE_MYMATH 是否存在带来的选择性
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

abaooo 发表于 2024-1-2 16:41
选择性是在h或cpp代码里体现的,相当于定义了一个宏给c/c++用

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
aoyu321 + 1 + 1 谢谢@Thanks!

查看全部评分

abaooo 发表于 2024-1-2 16:42
你在代码里搜索 USE_MYMATH 试试看,是不是有这个宏判断
 楼主| aoyu321 发表于 2024-1-2 17:05
abaooo 发表于 2024-1-2 16:42
你在代码里搜索 USE_MYMATH 试试看,是不是有这个宏判断

是的,是有的,也就是说,官方教程的这个注释写的是有问题的
chenzhigang 发表于 2024-1-2 18:28
aoyu321 发表于 2024-1-2 17:05
是的,是有的,也就是说,官方教程的这个注释写的是有问题的

没问题的 你看 TODO9-10
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-26 05:32

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表