What is the correct process for linking static lib

2019-06-20 02:33发布

问题:

I am working on a static library, called Silicon, that I use for all of my iOS apps.

Because I don't want to create one massive static library that could become hard to maintain I create lots of smaller static libraries that I attach as submodules.

As of the time of this writing the dependency tree for Silicon is as follows:

Silicon
|
|==> FDKeychain
|==> FDDataClient
        |
        |=> FDRequestClient
                |
                |=> FDFoundationKit
|==> FDSQLiteDatabase
        |
        |=> FDFoundationKit

As you can see both FDRequestClient and FDSQLiteDatabase have FDFoundationKit as a common static library.

What seems to happen is that when a project using Silicon is built it builds all of Silicon's target dependencies to the projects build directory. The same thing happens for FDDataClient and FDSQLiteDatabase. So at some point FDFoundationKit from FDRequestClient is built and copied to the build directory as well as FDFoundationKit from FDSQLiteDatabase. Whichever one is built last just overwrites the previous one.

Just by sheer luck FDFoundationKit hasn't been changing in any serious ways such that FDRequestClient and FDSQLiteDatabase can't always be using the same version but I can't guarantee it will be like this forever.

I am trying to figure out if there is a way for Silicon to specify which version of FDFoundationKit to use so it can be Silicon's responsibility to ensure that the the version used will work for both FDRequestClient, FDSQLiteDatabase and any other dependencies I add in the future.

I know CocoaPods attempts to solve this problem but I do not want to make someone have to setup all of that just to get my library working. If I could just find someway of having Silicon define which version of FDFoundationKit to use everything would work perfectly.

回答1:

You could (as we do) place all of your libraries into frameworks, as frameworks support versioning. Frameworks are just a directory tree configured in a common manner. Xcode does not directly support the creation of frameworks, so you have to create them in a script, typically as the last of your build phases. An example (thanks to jverkoey) can be found at IOS- framework

Within a framework you may store all versions of each static library within the

myLibrary.framework->Versions->n.n folders. 

The myLibary.framework->Versions->Current is a link to the folder of the latest version.

Since you are using static libraries, Silicon itself cannot specify the versions (that would require dynamic libraries), however the build, linker or environment flags used for the building of Silicon certainly can.

In this manner, by default, Apps will always use the latest version of the library, but the actual version linked may be easily overridden by linker flags when building. Also, all users will simply include the Silicon framework in their project in the same manner as any other framework, so it's very simple for developers.



回答2:

There looks to be only two answers to this problem:

1) Use a dependency manager like CocoaPods or Carthage.

2) Any static libraries or frameworks you release should not have any target dependencies. They should link against any dependencies you have and it is the responsibility of person integrating your library to also integrated the required dependencies.