为什么 -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
标志如何工作的理解,以下代码在面向 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
在 SecureTransport.h
中被标记为在 10.11 中可用:
$ 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。
我在这里误解了什么?
在 mac 上使用 XCode 10.0 (10A255)OS 此处为 10.13.6。
现在我可以回答我自己的问题了,我会:您需要将 -Wunguarded-availability
添加到您的编译标志中。只有这样你才会得到warning/error.
根据我对可用性宏和 -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
在 SecureTransport.h
中被标记为在 10.11 中可用:
$ 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。
我在这里误解了什么?
在 mac 上使用 XCode 10.0 (10A255)OS 此处为 10.13.6。
现在我可以回答我自己的问题了,我会:您需要将 -Wunguarded-availability
添加到您的编译标志中。只有这样你才会得到warning/error.