简体   繁体   中英

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!

Do static constructors work within the context of an iOS static lib?

They only start to work once they're linked into an executable, but once that is accomplished they will work.

How do you debug such a problem

There are a few approaches

  • You have to presume no specific order of the invocation of the constructors.
  • Make sure the constructors are being linked into the executable (use nm to determine this)
  • You can try an -Wl,-all_load to get it to load all the components of all archives into the executable (or -Wl,-force_load,libstatic.a to just load for a specific static archive).
  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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM