Using a static library in Xcode 4

Today I'd like to share some information on a topic that is dear to me: building static libraries with your Xcode projects.

I launched into testing something today and ran into issues I hadn't encountered under Xcode 3.  In the course of searching for the one right way to add static libraries to my project I found many forum and blog posts, all a little different from one another (and most from earlier this year).

Here are the steps for setting up my test project.

Set up your Workspace

First I created a static library project using Xcode's static library template (File...New...New Project...)

Next I created a workspace using Xcode (File...New...New Workspace). This is the preferred way of grouping related projects, and in this case I want to group my static library project with the project that relies on it.

Note that existing projects can be converted into workspaces (File...Save As Workspace...).

I created a new project called SimpleSearchDemo within the workspace. I then control-clicked under that project in the navigator to add my existing SimpleSearch static library .xcodeproj file.

A note: I repeatedly tried adding my static library project to this workspace but it kept appearing without the disclosure arrow.  After an hour or two I had a headache and restarted Xcode and tried again. It worked as expected. I still think about those hours sometimes. Collateral damage.

Edit your static library build settings

Next I selected the static library project to edit its build settings. I set it up to skip install and made sure the Copy Headers build phase included the headers. In particular, I added the public-facing header to the Public section.

Configure the project that uses the library

Once the static library was set up, it was time to configure the client project.

Link the static library with the project target; it should appear under 'Workspace' libraries.

Adjust the build settings so that the User Header Search Paths include the directory where the static library's headers are copied. I used the BUILT_PRODUCTS_DIR after reading this post. Both projects belong to the same Workspace, and thus share a common build directory.  Enable recursive searching of the user header search paths, and enable Always Search User Paths.

Finally, ensure the static library is built for archiving.

One other thing. I had issues with code completion working. I dragged the "public" header from the static library into the client project, and unchecked the 'Add to targets' checkbox.  Autocompletion started working (the header is right there, after all), but this seems unnecessary - let me know how you got around it.  Any other improvements are welcome.

Have you seen my apps? You might already use one.
© slidetorock 2009-2014