How to embed static library into Xcode 4 project

Dmitry Nikolaev, Apprium.ru 

Updated in February, 2012

1. Create Workspace

        1.1 In File->Workspace Settings set Build Location to “Place build products in derived data location” [1]

2. Create Static Library ‘SL’ Project [2]

3. Go to ‘SL’s target Build Settings, set ‘skip install’ to ‘YES’ [3]

        3.1 If you plan test static library and have categories in it, add argument to ‘Other Linking Flags’ and set it to ‘-ObjC’ in Test target

4. Create Cocoa Project ‘P’ (your main project)

5. Drag ‘SL’ project into ‘P’ [4]

6. Go to Build Settings of ‘P’ main target

        6.1 set ‘Header Search Path’ to Something like ‘../SL/SL’

        6.2 add argument to ‘Other Linking Flags’ and set it to ‘-ObjC’ [5]

7. Go to Build Phases of ‘P’ main target

        7.1 Add to Target Dependencies your ‘SL’ main target

        7.2 Add to Link Binary With Libraries your ‘SL.a’ library [6]

If you need include some resources with your static library

You cannot insert your xibs, images and so on in your static library. Instead, you should create resource bundle for them:

1. Choose ‘SL’ project. Press ‘Add New Target’ at the bottom. Choose ‘Bundle’ template, named it somehow (I use ‘RB’ further).

2. Copy or create new resources with pointing out that they are belongs to the ‘RB’ target

3. Add ‘RB’ as target dependency for ‘p’.

4. Drag your ‘RB’ from navigation pane under Products sub folder of ‘SL’ project to Copy Bundle Resources phase of main ‘P’ project.

6. Set up ‘Skip Install’ for RB to ‘YES’[7]

5. Enjoy your resources from bundle (don’t forget to load resources from new bundle, not Main Bundle)[8]

Known Issues


[1] During development your main application may be linked with old version of static library and you man get warnings like ‘more recent than executable timestamp in’ and ‘Could not open OSO file

[2] Ensure you have at least one class in library, you may have error if you have no classes in it.

[3] If you don’t set this, you may have no option for distrbute your app to App store with error “No Packager exists for the type of archive

[4] This is needed step, because if you change your static library sources, it will automatically recompiled

[5] Categories won’t work from static libraries if you dont set up this argument

[6] Maybe you need once build your static library, so you can choose SL.a from list

[7] Or you will face with separate archives for resource bundle and application

[8] Original article on how to make resources with static libraries on iOS