6

I am writing an iOS framework Foo that depends on a static third-party library Lib and have problems getting the linking right.

If Foo was a static library, I would not link it against Lib, and only link the master project against both of them. But this approach does not seem to work with a framework: the linker complains about missing symbols from Lib.

So I took the other approach and linked Foo against Lib. Now the linker is happy, but there’s an obvious catch: if the master project uses Lib for its own reasons and links both against Foo and Lib, I get duplicate symbols:

Class <Something> is implemented in both <Here> and <There>.
One of the two will be used. Which one is undefined.

I know I can stop linking the app against Lib and all will be fine, but I’d like to get things right. How?

zoul
  • 102,279
  • 44
  • 260
  • 354
  • Is this a dynamic framework? (support of which is only now being introduced into iOS AFAIK). – trojanfoe Jun 17 '14 at 13:57
  • It’s what Apple calls “private framework” on OS X. A framework that’s bundled inside the app’s package, not linked to its binary. Which probably means yes, it’s a dynamic framework. (Here’s a [question about the status of these frameworks on iOS](http://stackoverflow.com/questions/6758540).) – zoul Jun 17 '14 at 14:00
  • If that's the case then 1) you'll need to check support for the version of iOS you want to support (up to know there was only support for static frameworks which are glorified static libraries with associated resources). 2) duplicate symbols don't make sense as the binaries are completely separate (think .exe and .dll). – trojanfoe Jun 17 '14 at 14:03
  • Oh, it’s the runtime complaining, not the linker. Should have been obvious to me, I only get the warning at runtime. I’ll edit the question. – zoul Jun 17 '14 at 14:07
  • Ah I've seen that before and it sent a shiver down my spine (as it seems very insidious). However I cannot remember what the solution was :) – trojanfoe Jun 17 '14 at 14:09
  • @zoul i am struggling with this for some time, did you manage to find a solution? the accepted answer here dont seem to work. thanks – Michael A Aug 17 '15 at 14:23
  • Sorry, I can’t remember solving the issue, I have probably side-stepped it somehow. – zoul Aug 17 '15 at 15:45

1 Answers1

1

I was able to get this working for a framework, though the docs say it should work for a static library as well.

What I did is link the master project against both Foo and Lib as you say. Now Foo has "missing symbol" errors. Select the Foo target and go to Other Linker Flags. Add -weak_framework Lib and presto! the linker will stop complaining. The duplicate symbol runtime errors go away.

According to the docs:

The -weak_framework option tells the linker to weakly link all symbols in the named framework. If you need to link to a library instead of a framework, you can use the -weak_library linker command instead

bcattle
  • 12,115
  • 6
  • 62
  • 82