Ios – Best practice for static library dependencies


I'm writing a static library that has dependencies on other libraries (in my case SBJSON and ASIHTTPRequest).

If I compile these external dependencies into my library then I can't link against other libraries that have these classes compiled in. As my goal is to create a set of static libraries for my company that can be imported into any new app, compiling these dependencies into the library is obviously not an option.

Does anyone have any advice / best practices for creating a suite of shared static libraries with common dependencies?

Best Solution

A static library is just a collection of object files. In your case you don't want the object files for SBJSON and ASIHTTPRequest to be included in your static library—you want to leave that job to the final application. The only thing your static lib needs is the header files for SBJSON and ASIHTTPRequest.

Since these projects are both distributed as source files (.h and .m files) you just need to tell Xcode not to build the SBJSON/ASIHTTPRequest .m files for your static library target.

The easiest way to do this is to only import the .h header files for these projects into your Xcode project. Alternatively you can import both the .h and .m files but make sure the .m files are not included in the "Compile Sources" build phase of your static library target

Some other relevant SO topics:

How can I avoid "duplicate symbol" errors in xcode with shared static libraries?

Duplicate symbol: Include static lib A in static lib B, also include lib A and B in XCode Project