Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple static libraries with a shared dependency - iOS

I am working on several iOS projects which will have a good bit of shared code, so I'm collecting that code in static libraries.

Now let's say I have 3 static libraries I'm working with:

  1. Lib A contains generic utility functions
  2. Lib B contains one set of classes
  3. Lib C contains a second set of classes

(and A is a dependency of both B and C)

Now, my final project needs to utilize functionality from all three libraries. I know that A could be staticly linked with B, C, and my final project, but at that point I would get a linker error because there are multiple implementations of the same classes.

How should a situation like this be handled?

like image 256
jmac Avatar asked May 02 '26 09:05

jmac


1 Answers

For anyone else wondering about this, I've found that you can get the behavior I wanted by including header files with each library, but only linking with one.

In the example I outlined in my question, my primary project would link against A, B and C. However, to use A in libraries B & C, you can include the header files of A in B & C, but then only link against A in the root project.

If anyone has a better suggestion, please feel free to share.

As an aside, there's a project called Cocoapods that is an iOS dependency manager. It seems to be worth keeping an eye on.

like image 64
jmac Avatar answered May 05 '26 01:05

jmac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!