TechNote 6 – Roll your own static libraries

In TechNote 2, I outlined how to use static libraries from a Cocos2d project in one of your own projects, i.e. without actually copying the source into your project. In this technote, I describe how to package up some of your own code into a stand alone project and make it available to your other projects in the form of a static library. This assumes you have set up Xcode as described in TechNote 1 and are familiar with the procedures described in TechNote 2.

I have a utilities project, RMAUtils, that contains (surprisingly) utility classes and macros. I also have a Cocos2d project, RMACocos2d that builds come reusable controls based on Cocos2d and also uses RMAUtils.

Both projects were started as Cocoa Touch Static Library projects (New Project -> Library) and so they each produce static libraries named libRMAUtils.a or libRMACocos2d.a.

I also have an application project that uses my static libraries (libRMAUtils.a, libRMACocos2d.a) as well as Cocos2d. These (3) libraries were all included in the application project as described in TechNote 2.

This seemed to work fine for a while until I started using Categories in my RMAUtils project. After a bit of searching, I found I needed to include the following in the Linking section of the Target of my app.

Other Linker Flags: -all_load -ObjC

(Note the -all_load flag only seems to be needed on the device, not the simulator.)

However this caused multiple definition errors in the linking stage of the build.

It turned out I made the mistake of including libRMAUtils.a and Cocos2d static libraries in my RMACocos2d static library project in the “Link Binary with Libraries” stage of TechNote 2.

The solution simply involved not adding these libraries to linking stage in the static library projects, i.e. don’t do step 4 in TechNote 2 when building static library projects. The other steps are still required when setting up static library projects that rely on other projects.

The linking step (step 4) is only required in the final application project.


Leave a Reply

You must be logged in to post a comment.