C++ – pre-link static libraries for ios project

cioslinkerstatic-librariestemplates

I have a big iOS project that consists from several (about 20-30) static libraries that link together into final executable. Some of the components are platform-independent (pure C++) and some are iOS-specific (Obj-C/Obj-C++). C++ templates are used intensively, so each object file contains lots of symbols with vague linkage. The problem is that these symbols are merged only during linking of the final executable, but not when making static libraries. Each library contains tons of duplicated symbols (6-60 clones). So final linking of the app takes several minutes. This becomes extremely annoying when debugging and doing some small changes.

Is there a way to perform merging of the symbols with vague linkage for each library?

I known, that this is done automatically when using dynamic libraries. With some hacking (http://sumgroup.wikispaces.com/iPhone_Dynamic_Library) it is possible to build dynamic libraries for iOS. Is there a way to link dylib's statically (link them into a single executable file)?

Of course, being able to debug resulting app is a must.

Best Answer

You can pre-link your static library objects into a single one, also you can prelink other static libraries into one. It will actually link objects with the linker (almost like in dynamic library).

  1. In your single library (the main one) go to Build Settings and find Perform Single-Object Prelink in Linking sections. Switch that to Yes
  2. In Prelink libraries you can specify other libraries you want to include. There you need to put not just names, but full file name. If other libraries are also from your project, then you can use $(CONFIGURATION_BUILD_DIR) variable. So if you have library foo, then it will be $(CONFIGURATION_BUILD_DIR)/libfoo.a
  3. You can add additional flags in Single-Object Prelink Flags
  4. If you want to strip out local symbols, then make sure that you have Deployment Postprocessing set to Yes, as by default static libraries are not stripped out.