Search code examples
iosxcode4static-librariesworkspace

importing headers when compiling for osx & ios


Say i have two libraries "Foo" and "Bar", compiling as frameworks for OSX and static libraries for IOS.

Library 'Foo' depends on library 'Bar'. I'm using XCode4 workspaces.

In Ascii-art:-

  • IOS_App
  • ~ Foo.a
  • ~ ~ Bar.a

and

  • OSX_App
  • ~ Foo.framework
  • ~ ~ Bar.framework

Must file Foo.m that #imports Bar.h look like this:-

#if TARGET_OS_IPHONE
  #import "Bar.h"
#else
  #import <Bar/Bar.h>
#endif

Or, is there a way, maybe by copying headers when building Bar, and setting Search Paths appropriately, to make

  #import <Bar/Bar.h>

work in both cases?

(I suppose the root of my problem is not properly understanding the difference between #import </> and #import "")


Solution

  • I've done this sort of thing extensively, and what I ended up doing was modifying the prefix header for the application/project to import either the specific header file for the library/framework or if the prefix is common between OS X and iOS (such as for the Foo library/framework) adopt the first example in your question.

    Then in Foo.m you don't need to have any #import statements at the top.

    So, for example, in the prefix header for Foo.framework have

    #if TARGET_OS_IPHONE
      #import "Bar.h"
    #else
      #import <Bar/Bar.h>
    #endif
    

    and in the prefix header for your Mac OS X application have

    #import <Foo/Foo.h>
    

    and for iOS

    #import "Foo.h"
    

    Then all you source code, doesn't need to worry about the headers for any of the classes in your common libraries/frameworks.