在 Xcode 中使用 MACOSX_DEPLOYMENT_TARGET 对 Cocoa 应用程序进行条件编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4367988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:58:43  来源:igfitidea点击:

Conditional compilation using MACOSX_DEPLOYMENT_TARGET in Xcode for a Cocoa app

cocoaxcodemacosconditional-compilation

提问by user532477

In a Cocoa application, I'd like to use conditional compilation, like:

在 Cocoa 应用程序中,我想使用条件编译,例如:

#if MACOSX_DEPLOYMENT_TARGET <= MAC_OS_X_VERSION_10_4    
    [[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
#else
    [[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
#endif

My hope is that this will avoid compiler warnings about removeFileAtPath: being deprecated when MACOSX_DEPLOYMENT_TARGET = 10.6, since it shouldn't be compiling that line.

我希望这将避免编译器警告关于 removeFileAtPath: 在 MACOSX_DEPLOYMENT_TARGET = 10.6 时被弃用,因为它不应该编译该行。

It doesn't work.

它不起作用。

When MACOSX_DEPLOYMENT_TARGET = 10.6 I get a warning that removeFileAtPath: is deprecated. But it shouldn't be compiling that line, so it shouldn't be warning about it having a deprecated method!

当 MACOSX_DEPLOYMENT_TARGET = 10.6 时,我收到一条警告,提示 removeFileAtPath: 已弃用。但它不应该编译该行,因此不应该警告它有一个已弃用的方法!

(I am setting MACOSX_DEPLOYMENT_TARGET in both the project build settings and the target build settings. I have BASE_SDK set to 10.6 and specify GCC 4.2 in both, too.)

(我在项目构建设置和目标构建设置中都设置了 MACOSX_DEPLOYMENT_TARGET。我将 BASE_SDK 设置为 10.6 并在两者中指定 GCC 4.2。)

What am I doing wrong? Do I have some fundamental misunderstanding of conditional compilation?

我究竟做错了什么?我对条件编译有一些基本的误解吗?

回答by Laurent Etiemble

MACOSX_DEPLOYMENT_TARGETis mostly used to perform a weak-linking. You should use MAC_OS_X_VERSION_MIN_REQUIREDinstead to perform conditional compilation:

MACOSX_DEPLOYMENT_TARGET主要用于执行弱链接。您应该使用MAC_OS_X_VERSION_MIN_REQUIRED代替来执行条件编译:

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
    [[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
#else
    [[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
#endif

See Ensuring Backwards Binary Compatibility - Weak Linking and Availability Macros on Mac OS Xfrom Apple for more examples.

有关更多示例,请参阅确保向后二进制兼容性 - Mac OS X 上的弱链接和可用性宏