本帖最后由 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) |