Search code examples
ioscclangllvm-clang

iOS + C: the use of __attribute__ ((__constructor__)) in static framework


I did quite a bit of googling for a definitive answer, but I could not find one.

We have cross-platform sources that need to be used by our iOS apps. I have already packaged them as static framework and got a test app to link successfully against and make a call into it.

The problem I am having is that it makes heavy use of global static constructors.

I am seeing erratic behavior with only a subset of objects getting instantiated but not all.

My questions:

  1. Do static constructors even work within the context of an iOS static lib?
  2. How would one debug such a problem?

Thanks!


Solution

    1. constructors in a static library get called erratically
    2. The use of -all_load and -force_load does not make any difference in invocation of constructor attribute labeled functions
    3. What you need to do is this:
      • make sure the constructors are global symbols. do this by exporting them via a symbols' file.
      • consider creating an init function and use the linker setting "initialization routine" to control the order of the instantiation of your global objects..