为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?

Why isn't -mmacosx-version-min=10.10 preventing use of a function tagged as starting in 10.11?

本文关键字:记为 开始 函数 -mmacosx-version-min 为什么      更新时间:2023-10-16

根据我对可用性宏和-mmacosx-version-min标志如何工作的理解,以下代码在面向OS X 10.10时应该无法编译:

#include <Availability.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
#if !defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
#error
#endif
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101000
#error __MAC_OSX_VERSION_MIN_REQUIRED too low
#endif
#if __MAC_OS_X_VERSION_MIN_REQUIRED > 101000
#error __MAC_OSX_VERSION_MIN_REQUIRED too high
#endif
int main() {
size_t len = 0;
SSLContextRef x{};
auto status = SSLCopyRequestedPeerNameLength(x, &len);
return status != 0;
}

因为函数SSLCopyRequestedPeerNameLength在 10.11 中被标记为在SecureTransport.h中可用:

$ grep -C5 ^SSLCopyRequestedPeerNameLength /System/Library/Frameworks//Security.framework/Headers/SecureTransport.h
/*
* Server Only: obtain the hostname specified by the client in the ServerName extension (SNI)
*/
OSStatus
SSLCopyRequestedPeerNameLength  (SSLContextRef  ctx,
size_t         *peerNameLen)
__OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0);

然而,当我使用-mmacosx-version-min=10.10在命令行上编译时,我根本没有收到任何警告,尽管-Wall -Werror -Wextra

$ clang++ -Wall -Werror -Wextra ./foo.cpp --std=c++11 -framework Security -mmacosx-version-min=10.10 --stdlib=libc++ ; echo $?
0

我是否需要提供一些其他定义或特定警告才能启用以确保我不会选择对高于 10.10 的 API 的依赖关系?我真的期望-mmacosx-version-min=10.10会阻止使用标有更高版本号的 API。

我在这里误解了什么?

在此处在 macOS 10.13.6 上使用 XCode 10.0 (10A255(。

现在我可以回答我自己的问题,我将:您需要将-Wunguarded-availability添加到编译标志中。只有这样,您才会收到警告/错误。