如何用 C 定义 macOS 的特定版本

The*_*xom 5 c macos makefile

我们可以使用#ifdef _linux_或者#ifdef __APPLE__来识别用户的操作系统。但是我们可以定义不同版本的操作系统吗?例如 macOS Big Sur 和 macOS Monterey 之间。

在寻找答案时,我遇到了Availability.h库,但我不太明白如何找到正确的操作系统版本

JWW*_*ker 5

您可以在运行时使用 C/C++ 代码测试操作系统,如下所示:

if (__builtin_available( macOS 12.0, * ))
{
    // Monterey or later
}
else
{
    // before Monterey
}
Run Code Online (Sandbox Code Playgroud)

当然,你必须考虑 macOS 13 发布后会发生什么。