Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can preprocessor directives be used to import different header files for Mac and iOS?

I am writing a class library for Mac OS X and iOS to be released as a Cocoa Framework for OS X and a Static Library for iOS. To simplify matters, I intend to use multiple targets in Xcode. However, the classes on Mac OS X link against Cocoa.h whereas on iOS they link against Foundation.h.

My questions basically are:

  • Could the Mac OS X framework link against Foundation.framework instead? Classes used within the framework are NSString, NSMutableString, and NSMutableArray.
  • Or could I use preprocessor directives within the header files to control framework inclusion, e.g.

    #ifdef MacOSX
        #import <Cocoa/Cocoa.h>
    #else
        #import <Foundation/Foundation.h>
    #endif
    
like image 629
BWHazel Avatar asked Jan 25 '11 19:01

BWHazel


People also ask

What is the difference between header file and preprocessor directives?

In general a library is made of header files + (but optionally) of compiled code. With preprocessor you include header files, compiled code is linked to your code (nothing to do with preprocessor).

What is the preprocessor directive for the input and output header?

Given below is the most commonly included header file which contains the standard input/output functions like, printf(), scanf(), etc. Here, the symbol # is called the preprocessor directive, include is called the command, and stdio. h is the header file. Note: Some compilers automatically include stdio.


1 Answers

You can use these to separate platform dependent code (see TargetConditionals.h):

#ifdef TARGET_OS_IPHONE 
    // iOS
#elif defined TARGET_IPHONE_SIMULATOR
    // iOS Simulator
#elif defined TARGET_OS_MAC
    // Other kinds of Mac OS
#else
    // Unsupported platform
#endif

Here's a useful chart.

like image 59
detunized Avatar answered Oct 21 '22 13:10

detunized