Skip to content

Instantly share code, notes, and snippets.

@gdavis
Created June 12, 2012 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdavis/2918964 to your computer and use it in GitHub Desktop.
Save gdavis/2918964 to your computer and use it in GitHub Desktop.
How to use Static Libraries under Xcode 4
Create a Workspace
Under Xcode 4, go to File, New, Workspace.
From Finder you can then drag in the .xcodeproj projects for both the static library you want to use, and the new app you are building that uses the library. See Apple Docs for more info on setting up Workspaces: http://developer.apple.com/library/ios/#featuredarticles/XcodeConcepts/Concept-Workspace.html
Static Library Project Settings
Make sure all the static library's headers are set to copy to "Public". This is done under the settings for the static library target > Build Phases. In the "Copy Headers" phase, make sure all your headers are within the "Public" section.
Next go to Build Settings, find "Public Headers Folder Path" and type in a path for your library. I choose to use this:
include/LibraryName
I've adopted this from use with RestKit and found it works best with all my static libraries. What this does is tells Xcode to copy all the headers we moved to the "Public" headers section in step 1 to the folder we specify here which resides within the Derived Data folder when building. Like with RestKit, I like using a single "include" folder to contain each static library I'm using in a project.
I also don't like using macros here because it will allow us to use a single user header search path later when we configure the project using the static library.
Find "Skip Install" and make sure this is set to YES.
Settings for the Project Using the Static Library
Add the static library as a framework under Build Phases > Link Binary With Libraries and add the libLibraryName.a file for whatever static library you want to use.
Next make sure project is set to search for User Search Paths. This is done under Build Settings > Always Search User Paths and make sure its set to YES.
In the same area find User Header Search Paths and add:
"$(PROJECT_TEMP_DIR)/../UninstalledProducts/include"
This tells Xcode to look for static libraries within the intermediate build folder that Xcode creates during the build process. In here, we have the "include" folder we are using for our static library locations we setup in step 2 for the static library project settings. This is the most important step in getting Xcode to correctly find your static libraries.
Configure the Workspace
Here we want to configure the workspace so that it will build the static library when we build our app. This is done by editing the scheme used for our app.
Make sure you have the scheme selected that will create your application.
From the scheme drop-down, choose Edit Scheme.
Select Build at the top of list on the left. Add a new target by pressing the + on the middle pane.
You should see the static library show up for the library you are trying to link. Choose the iOS static library.
Click both Run and Archive. This tells the scheme to compile the libraries for the static library whenever you build your app.
Drag the static library above your application target. This makes the static libraries compile before your application target.
Start Using the Library
Now, you should be able to import your static library using
import <LibraryName/LibraryName.h>
This method gets around the hassle of having to have different user header paths for different configurations, so you should have no problem compiling for archives.
Why does this work?
It all depends on this path:
"$(PROJECT_TEMP_DIR)/../UninstalledProducts/include"
Because we configure our static library to use "Skip Install", the compiled files are moved to the "UninstalledProjects" folder within the temporary build directory. Our path here also resolves to the "include" folder we setup for our static library and use for our user header search path. The two working together lets Xcode know where to find our library during the compile process. Since this temporary build directory exists for both Debug and Release configurations, you only need a single path for Xcode to search for static libraries.
Hope this helps, have fun!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment