My Xcode project "MyMainApp" has a static library type Xcode subproject called "MyLib". Now I need to use some third party iOS frameworks in my iOS app. I want to add/link them against only MyLib and not against MyMainApp so I can make the MyLib reusable and self-contained to use it in my other iOS apps too. A few view controllers in MyMainApp use one of the view controller that is a part of MyLib. This view controller in MyLib uses functionality from the classes that reside inside third party framework. Now the problem I am facing is if I add the third party frameworks to just MyLib and don't add them to MyMainApp, I get a linker error "Lexical or Preprocessor issue. XXX.h file not found". Everything works well if I addd the frameworks to both MyMainApp and MyLib but this is not what I want. I have made sure that the framework search paths and header search paths are correct. I've been unable to find any reference from Apple in such scenario. I'd like to know the best practice for adding/linking third party frameworks and libraries to an Xcode subproject of type static library. Also is there a solution to overcome the linker error and add frameworks to only the static library project?
3 Answers
Your library will have to link against them, and applications using your library will have to link against them and include them in the application bundle. The application is responsible for satisfying the dependancy.
Frameworks can contain resources (like the header files that are giving you issues), which is one of the primary things that differentiates them from libraries. Because of this, a library can't really contain a framework, even if it could contain the framework's binary objects (code).
If your library is converted to a framework it CAN contain other frameworks within it, which may meet your needs.
My Xcode project "MyMainApp" has a static library type Xcode subproject called "MyLib". Now I need to use some third party iOS frameworks in my iOS app. I want to add/link them against only MyLib and not against MyMainApp so I can make the MyLib reusable and self-contained to use it in my other iOS apps too.
Libraries don't link themselves against other libraries, they just get build separately into unlinked *.a binaries. Later the application (and only then) will link all those *.a files and its own binaries together.
You're right though that libraries should be kept modular and do not include other libraries code or resources to avoid version and compatibility problems.
Now the problem I am facing is if I add the third party frameworks to just MyLib and don't add them to MyMainApp, I get a linker error "Lexical or Preprocessor issue. XXX.h file not found". Everything works well if I addd the frameworks to both MyMainApp and MyLib but this is not what I want.
Your application is indirectly trying to import one of the 3rd party headers. While you don't need to reference the 3rd party library code you still need to have access to its header files to compile. In other words you don't need to add the 3rd party library to your application but you do have to add the header files or set the header search path.
I'd like to know the best practice for adding/linking third party frameworks and libraries to an Xcode subproject of type static library. Also is there a solution to overcome the linker error and add frameworks to only the static library project?
There's no easy "native" way to handle libraries, their resources and dependencies on iOS, so you should take a look at CocoaPods.