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:
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
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With