Monday, December 25, 2006

Static Libraries dependent upon static libraries in XCode

Just a helpful hint to try and prevent your wasting time like we did at my day job. We have a large C++ application which has several different levels (Standard, Pro, Ultra....). So to speed compilation and linking, we put large chunks of code common to all the levels into separately compiled static libraries. Thus, we only have to compile that code once every time we build all the applications, resulting in a massive time savings.

Unfortunately, this was not working under XCode as when we linked in our libraries, there was always some symbol or other which was not available, and we ended up putting large amounts of code into the main project and compiling it for each application level (making our build process at least twice as long as it should have been).

My boss finally got so sick of this he spent a couple days tracking down the problem, and it was that our static libraries were dependent upon other static libraries, but those libraries were not linked in until the application as a whole was linked. Somehow, this confused the XCode linker (ld) and it ended up missing symbols. The trick was to link the other sub-dependent libraries into the libraries static libraries and not directly from the main project.

I know the above is confusing. Here's a more graphical description.
(Before)

  • Main Project

    • Static Library 1 Project (needs Static Library 2 & 3)

    • Static Library 2 Project

    • Static Library 3 Project


(Now)
  • Main Project

    • Static Library 1 Project (needs Static Library 2 & 3)

      • Static Library 2 Project

      • Static Library 3 Project