Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing inclusion of static library object files that don't export any symbols (GCC/iPhone)

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.

like image 370
Richard Viney Avatar asked Mar 01 '23 07:03

Richard Viney


2 Answers

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.
like image 120
F'x Avatar answered Mar 05 '23 16:03

F'x


-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
like image 43
Anna B Avatar answered Mar 05 '23 16:03

Anna B