Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link static library inside dynamic (Cocoa Touch) framework

I have a dynamic framework which links against Google Maps (which is, as far as I see it, still kind of a static library, if not exactly that, just with a framework wrapper).

The problem is that the framework links against the static library and also seems to include its code directly, as I don't need to link or embed Google Maps in the app that uses the framework and everything works fine. Except if I work with Google Maps inside the app as well.

Either I get "undefined symbols for architecture XY" during the compile phase, or I link Google Maps against it and then I get a wall of warnings in the debug-console during app-launch like:

Class GMSPolyline2D is implemented in both /.../Build/Products/Debug-iphonesimulator/MyFramework.framework/MyFramework and /..../Bundle/Application/7C73324B-4A42-4063-98AA-879345997DE6/MyApp.app/MyApp. One of the two will be used. Which one is undefined.

Is there any way to only link against Google Maps in the framework, but NOT to include the symbols in the framework-product as well? It works for dynamic frameworks, but static libraries seem to be just included. I already tried weak-linking with -weak_framework and -weak_library, but it does not seem to work.

Actually, I could not make Xcode find the GoogleMaps binary for -weak_library inside its framework dir, even with directly linking inside the .framework folder as Library Search Path.

Since I am the only user of my framework I can enforce that every app that uses the framework DOES include the Google Maps framework. So far the app works fine except the warnings (and so far it does not matter which of both classes is used at runtime, since both should be the same). However, I would sleep better if there was a nicer solution :)


Edit: I could get -weak_library to work by just handing over the absolute path directly in the linker-flag section instead of using the Library Search Path setting. However, the problem still remains, obviously it is still included in the framework.

like image 737
Luke Schwoebel Avatar asked Nov 09 '15 20:11

Luke Schwoebel


People also ask

Can a static library use a dynamic library?

Yes for instance when you call windows functions from within your static lib they are normally from some dynamic library so there should be no difference.

How are static libraries linked?

Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.

Are CocoaPods static or dynamic?

CocoaPods pod-linkage plugin. In SwiftKey, we have a dynamic framework that we use to share code between the app and the keyboard extension, and all the pods are linked statically except for some of them that are linked dynamically because they are linked to the app and keyboard extension too.


1 Answers

Don't Do Dat

The best practice is to place all your Google Maps-dependent code in a separate library that links to both your library and the Google Maps static library.

(Credit: Robert Napier's answer to How to compile static library that includes optional classes that depend on a third party library

like image 174
Montana Burr Avatar answered Oct 07 '22 11:10

Montana Burr