【问题标题】:Why isn't -mmacosx-version-min=10.10 preventing use of a function tagged as starting in 10.11?为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
【发布时间】:2019-03-29 09:01: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。

我在这里误解了什么?

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

【问题讨论】:

  • 好的,找到了:你需要传递魔法标志-Wunguarded-availability,它在任何地方都没有记录。

标签: c++ xcode macos clang abi


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-10
    • 2012-12-25
    • 2021-11-22
    • 2023-03-14
    • 2021-06-17
    • 2021-06-27
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多