Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Conditionally Compile for Different SDKs

If you will be building the same source code using different SDKs, you may want to compile code conditionally based on the SDK in use. You can do this by using preprocessor directives with the macros defined in AvailabilityMacros.h.

Important: AvailabilityMacros.h is included automatically by the Carbon and Cocoa frameworks in the SDKs for 10.2 and later, but not by the 10.1 SDK. If you wish to use these macros with the 10.1 SDK, you should manually include the file, typically by adding the following line to your project's prefix header:

#include <AvailabilityMacros.h>

If you fail to add this line, code that builds successfully with the other SDKs will fail silently when built with the 10.1 SDK.

For example suppose the function shown in Listing 2-1 must be compiled against the 10.2.8 SDK (or by other developers using 10.2 and no SDK). Even referring to the HIAboutBox function will cause a compiler failure, so you must exclude that code altogether unless the 10.3 header files are in use. You could do so with code like the following:

void MyAboutBox(void)
{
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
    if(HIAboutBox != NULL)
    {
        HIAboutBox(NULL);
    }
    else
    {
#endif
        // Lots of code to display an about box with earlier technology.
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
    }
#endif
}

With this technique, the source file can be built on native 10.1 or 10.2 systems (or using the 10.1 or 10.2 SDKs) without compilation errors.



< Previous PageNext Page > Hide TOC


Last updated: 2006-11-07




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice