Question

TL;DR

  • How do I build a static library without including a third party library that I'm referencing?

Hi,

Before I begin, I know how to compile a static library in Xcode targeted for iOS devices and simulators. However, this time around I have a dependency on a third party library that I do not want to include in the static library. I simply want to reference it. However, whenever I am building my static library I get a lexical or preprocessor error, which I know is common when the library is missing from the project. The error occurs because I removed the library from the project, because I didn't want to have it built into the static library.

The library in question is AFNetworking. I will be using CocoaPods to explicitly have the dependency installed when installing my static library.

Thank you.

Was it helpful?

Solution

When you reference external code, you will always need to have the headers accessible by your project, otherwise the compiler wouldn’t be able to tell you if you are referencing the external code in a proper manner. E.g. does the method exist, are you providing the right types, etc.

The duplicate symbol problem only exists when you actually define a symbol multiple times. For instance, defining class related Objective-C symbols only happens in a @implementation...@end block, not in a @interface...@end block. The latter is only for the compiler to make sense of things.

(This is also why you can define a @interface for e.g. a private class and use it as normal and not cause duplicate symbols.)

Therefore, you can include such ‘clean’ headers multiple times without having to worry about it. Note that I said ‘clean’, because you can actually define symbols in headers by, for instance, defining C functions in headers or even a Objective-C @implementation, if you feel especially wicked. But you will simply have to test this.

The linker will complain when you finally link the 3rd-party dependency and yours and there were duplicate symbols after all. In that case, be sure to look into tools such as nm which list the symbols in an archive.

OTHER TIPS

you can add the lib.a and lib.h into your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top