据我了解可用性宏和-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。
我在这里误解了什么?
此处在macOS 10.13.6上使用XCode 10.0(10A255)。
答案 0 :(得分:2)
现在我可以回答自己的问题,我将:您需要在编译标志中添加-Wunguarded-availability
。只有这样,您才会收到警告/错误。