Compile several static libraries into a single iOS Framework
c, ios, iphone, libtool-xcode, static-libraries
Solution
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).
- In your single library (the main one) go to Build Settings and find Perform Single-Object Prelink in Linking sections. Switch that to Yes
- 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
- You can add additional flags in Single-Object Prelink Flags
- 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.
If your libraries have duplicate symbols, then there is something wrong in the structure of your modules.
- If they are the same functions, then separate them in a separate static library and reuse it from other places.
- If those symbols are actually different and just have the same name, then consider declaring them as static (if it is ANSI C). You can also
- You can hyde some local symbols with setting visibility (http://llvm.org/docs/LangRef.html#visibility-styles, http://gcc.gnu.org/wiki/Visibility)
Problem
I have a big static library I would like to package into an iOS static framework for ease of use. The library is actually several `.a` files, one per logical module of the library. I have the compiled `.a` files built for the device (fat file with `armv7`, `armv7s`, `arm64`) and for the simulator (fat file with `i386`, `x86_64`). Now, AFAIK, I need to combine all those files into one big file in order to build a proper framework. Using the technique described here (Combine static libraries), I could do : ``` libtool -static -o new.a old1.a old2.a ``` But apparently both `old1.a` and `old2.a` include the same symbols. Thus, when linking against my framework, I get the linker error (for a valid reason) `duplicate symbols`. A more correct way to do that (thus avoiding duplicate symbols) seems to be unpacking the `.o` files, and combining them into a big `.a` file (How to pack multiple library archives (.a) into one archive file?) ``` ar x old1.a ar x old2.a ar rcs new.a *.o ``` Now, remember, `old1.a` and `old2.a` are fat files, so I need to separate that per architecture. So here's what I do: ``` lipo old1.a -thin armv7 -output armv7/old1.a cd armv7; ar x old1.a; cd .. ... lipo old1.a -thin x86_64 -output x86_64/old1.a cd x86_64; ar x old1.a; cd .. // Same goes for old2.a ... // Then, libtool -static -o new.a armv7/*.o armv7s/*.o arm64/*.o // ... etc ``` But for some reason ,when linking against the thus created framework, the linker can't find any symbol (even though `nm` reveals them all). Any idea how to build that static framework?