views:

467

answers:

2

Hi,

I'm creating a static library for use in iPhone applications. It contains a number of source files which export no symbols, all they do is instantiate a static instance of a class which then runs some code in its constructor that registers various things with a central manager class elsewhere. This all works fine when the code is built as part of a standard iPhone application, but when it is separated into a static library these files don't make it into the final application binary and so the constructors for the private class instances they contain don't get run, and this causes problems. I've turned off every build option to do with dead stripping and so on for both the static library build and the final application build.

I ran into this issue on the Metrowerks compiler a while ago, however in that instance it was occurring even when the code was built into a single application without any intermediate libraries. The solution was quite straightforward: just use __declspec(force_export) on the private class instances and all is well.

Is there any equivalent for GCC/iPhone? I'm using Xcode 3.1.4 with GCC 4.2 and targeting iPhone OS 3.1. Alternatively is there some way to tell the application to link in every object file in the static library regardless of whether or not it's explicitly referenced? I've confirmed using ar that the full set of object files are making it into the static library.

Thanks in advance.

+3  A: 

I think the option you are looking for is -all_load:

   -all_load
       Loads all members of static archive libraries.  See man ld(1) for
       more information.
FX
Fantastic! That did exactly what was needed :-).
richard_nz
+1  A: 

-all_load has some issues if you happen to link many libraries and/or frameworks (Mac OS X). In this case you should only force all symbols from a specific library by using -force_load:

g++ test.cpp -o test -force_load libtoload.a
Gaspard Bucher
Hmmm... after some testing, it seems "force_load" doesn't work for me (Mac OS 10.6.2). But "all_load" finally worked ok with the frameworks.
Gaspard Bucher