Conditional compilation using MACOSX_DEPLOYMENT_TARGET in Xcode for a Cocoa app

cocoa, conditional-compilation, macos, xcode

Solution

`MACOSX_DEPLOYMENT_TARGET` is mostly used to perform a weak-linking. You should use `MAC_OS_X_VERSION_MIN_REQUIRED` instead to perform conditional compilation:

#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 X from Apple for more examples.

Problem

In a Cocoa application, I'd like to use conditional compilation, like: ``` #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. 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! (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.) What am I doing wrong? Do I have some fundamental misunderstanding of conditional compilation?

Original source