Create and use a static library

After some time developing iphone applications I was starting to have a huge codebase that was shared between several projects. In order to avoid to mantain the same classes files to all the projects I started to look for a way to create a shared library. After some “google” around I found some techniques, but the one more accepted by the community and recommended by Apple was the use of static libraries.

How to create and use them? Just follow these instructions:

Create a new project in the XCode using the template static library. In this example we will create a project called MyLibrary. Make sure you store the project in a permanent place because Xcode will need to reference the files every time you compile your project.
Add to this project all the files that you want to use in your custom library. After you have completed just build the project to check if you have no errors in you library.

After you have you library ready you will want to use in other projects for that locate in Finder the “MyLibrary.xcodeproj” file under your project folder. Drag “MyLibrary.xcodeproj” and drop it onto the root of your Xcode project’s “Groups and Files” sidebar. A dialog will appear — make sure “Copy items” is unchecked and “Reference Type” is “Relative to Project” before clicking “Add”.

Now you need to link the your new static library to your project. Click the “MyLibrary.xcodeproj” item that has just been added to the sidebar. Under the “Details” table, you will see a single item: libMyLibrary.a. Check the checkbox on the far right of libMyLibrary.a.

Now you need to add your library as a dependency of your project, so Xcode compiles it whenever you compile your project. Expand the “Targets” section of the sidebar and double-click your application’s target. Under the “General” tab you will see a “Direct Dependencies” section. Click the “+” button, select “MyLibrary”, and click “Add Target”.

Finally, we need to tell your project where to find your library headers. Open your “Project Settings” and go to the “Build” tab. Look for “Header Search Paths” and double-click it. Add the relative path from your project’s directory to the “MyLibrary/src” directory.

While you are in Project Settings, go to “Other Linker Flags” under the “Linker” section, and add “-ObjC” and “-all_load” to the list of flags.

You’re ready to go. Just #import files from you library anywhere you want to use MyLibrary classes in your project.

References:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close