0

I have a problem that I haven't been able to resolve in regards to when I link my iOS App against one or more static libraries. Here is the issue:

I am creating several static libraries (MACH-O type: Relocatable Object File) each of which contain a large number of symbols. Each of these static libraries are FAT libraries, containing a slice for each iOS/Simulator architecture. From my understanding, when I compile my iOS Application only the symbols that are used by my App should be compiled into and included in my App, however, ALL of the symbols are being included.

I've done some testing using otool, nm, and other tools and can see that when I link against any of these libraries, even if I don't call any of the code in the libraries, ALL of the symbols are being compiled into the App. It takes my App from 42kB all the way to 3+MB.

Any ideas on why this is happening?

2 Answers2

0

Objective-C is a dynamic runtime; it is permissible e.g. to perform:

NSString *classToUse = ["MPViewController" stringByAppendingString:class];
return [[NSClassFromString(classToUse) alloc] init];

... and furthermore this is more or less exactly what happens when you load a NIB — string class and property names are loaded from disk, the backing classes are then found via the runtime and properties applied by key-value coding. So dynamic lookup is not an edge case.

A linker can therefore not make any assumptions about which symbols are used from an Objective-C static library, unlike e.g. a C linker.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

Yes,It can be done by stripping static libraries, but if post-processing is enabled. Set Xcode build setting "Deployment Postprocessing" to yes. (DEPLOYMENT_POSTPROCESSING=YES). Also make sure that "Use separate strip" is set to Yes. You can check this

You can also achieve what you are expecting by using Dynamic Library, there is a very nice article here

Extra Tip * While creating library set Debug Symbols to NO in your build settings. This can reduce the size of your static library by up to 30%.

Community
  • 1
  • 1
Saurav
  • 537
  • 4
  • 20
  • The problem isn't that there's a lot of symbols in the static library (needed to link along with the other libs), it's just that ALL of the symbols are being included in my App. For example, I have two static libraries, _lib1_ and _lib2_. _lib1_ exports a global symbol (say _gsym_ that is used by _lib2_. When I link my App with _lib1_ (and don't call any code), then _gsym_ isn't used in my application, or indirectly by _lib2_, therefore it shouldn't be included in my final App, however, this is the issue that I am experiencing. Any other suggestions? – SomeRandomiOSDev Apr 19 '16 at 18:08