Conditional compilation using MACOSX_DEPLOYMENT_TARGET in Xcode for Cocoa application

In a Cocoa application, I would like to use conditional compilation, for example:

#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 prevent the compiler from warning about removeFileAtPath: it is deprecated when MACOSX_DEPLOYMENT_TARGET = 10.6, since it should not compile this line.

This does not work.

When MACOSX_DEPLOYMENT_TARGET = 10.6, I get a warning that removeFileAtPath: is deprecated. But he should not compile this line, therefore he should not warn about obsolete methods!

(I set MACOSX_DEPLOYMENT_TARGET both in the project build settings and in the target build settings. I have BASE_SDK set to 10.6 and also indicates GCC 4.2 in both.)

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

+7
xcode cocoa conditional-compilation macos
source share
1 answer

MACOSX_DEPLOYMENT_TARGET is mainly used for weak communications. You should use MAC_OS_X_VERSION_MIN_REQUIRED instead to conditionally compile:

 #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 Backward Compatibility of Binary Compatibility - Apple Mac OS X Addition and Availability Macros for more examples.

+11
source share

All Articles