5

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?

indiantroy
  • 1,503
  • 1
  • 15
  • 25
  • Trying to understand your question here. Are you trying to use/distribute a framework that links 3rd party frameworks but users of your framework will not have to link or even be aware of the 3rd party frameworks? Kind of package it all up into one tidy framework? – Patricia Aug 06 '14 at 20:20
  • @Lucy, I am trying to use a static library in my Xcode project and the static library links to other 3rd party frameworks. What I want to know is if it is possible that only the static library links against the 3rd party frameworks and my Xcode project just links against the static library and not against the 3rd party frameworks. Currently I have to link both my Xcode project and my static library against the 3rd party frameworks otherwise I get linker errors. – indiantroy Aug 06 '14 at 20:32
  • OK. I think we're on the same page, maybe. I want to do the same thing. Currently, I have a Universal Framework that does link in 3rd party Frameworks. And, YES, applications that use my Framework also must link in the 3rd party Frameworks. I haven't tried adding the 3rd party Frameworks to the Bundle Resources yet. I've run across the term 'Embedding' Frameworks in your Framework....not sure what that means. If I find an answer, I'll let you know. Please do the same for me. :-) – Patricia Aug 06 '14 at 20:44
  • Hi @Lucy , have you find the solution to link third party framework into the framework? – shoujo_sm Mar 04 '15 at 01:51
  • Hi @shoujo_sm, I'm still working on it on the side. My company has me working on embedded C++ code right now. I'll let you know if I figure it out and please let me know if you figure it out. – Patricia Mar 04 '15 at 15:23

3 Answers3

1
  1. Go to 'Build Phases' for 'MyLib'
  2. Add the '3rd party iOS Framework' under 'Copy Bundle Resouces'
  3. Compile 'MyLib'
  4. Remove '3rd party iOS Framework' from 'MyMainApp'
  5. Run 'MyMainApp'
  • I followed the steps exactly as you mentioned but got linker errors "Undefined symbols.." while running MyMainApp. – indiantroy Jun 24 '14 at 21:18
1

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.

quellish
  • 21,123
  • 4
  • 76
  • 83
1

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.

Jeremy Wiebe
  • 3,894
  • 22
  • 31
Rivera
  • 10,792
  • 3
  • 58
  • 102
  • Not linking the 3rd party library to MyMainApp and just setting the search paths don't work either and result in linker error. – indiantroy Jun 24 '14 at 21:27
  • Yes, you link everything to your App. On the other hand "I want to add/link them against only MyLib and not against MyMainApp" is wrong. – Rivera Jun 26 '14 at 05:50