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

标签 c++ xcode macos clang abi

根据我对可用性宏和 -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;
}

因为函数 SSLCopyRequestedPeerNameLengthSecureTransport.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++ - 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52977581/

相关文章:

c++ - 在可变参数模板中实现 STL 函数

c++ - 打印浮点值时出现问题

c++ - 相互依赖的 C++ 类,由 std::vector 持有

ios - 在 Storyboard xcode 7 中为具有不同宽度的两个 View 设置常量

xcode - 如何重构 localizable.strings?

ios - 在 Xcode 10 上使用 Date Picker 对象获取时间输入

css - 如果 outline-style 设置为 "solid"(而不是 "auto"),则 Chrome 上的轮廓绘制不正确

c++ - 在 C++ 中构建 libcurl 库,Noob 问题

macos - 查找所有带有特定 'color' 标签的 OSX 文件

c++ - 我在 Mac OS X 上的 .dylib 文件在哪里?