Link binary with static library in xcconfig

2019-04-29 14:54发布

问题:

I have an Xcode project that is essentially a single app that gets built into several almost identical targets. I have moved nearly all build configuration settings in central places, but I cannot figure out how to move linking against a static library (libMantle.a) to the xcconfig. I have tried the -framework and -l flags, like I'm using for various other libraries, but they don't work. Is there a way to get .a files out of the Link Binary with Libraries pane, so I don't need to keep all the targets in sync manually?

回答1:

Figured it out...

Let's say you want to link against the file libGoogleAnalyticsServices.a. There are basically three things you should have in your .xcconfig to get it working.

First you want to make sure you have the path to the library in your search path. Then you need to pass two flags to make the linker happy -L with the path to the directory, and -l with the library.

Putting it all together will get you something like:

LIBRARY_SEARCH_PATHS = $(inherited) "$(SRCROOT)/Vendor/Google Analytics"
OTHER_LDFLAGS = $(inherited) -L"$(SRCROOT)/Vendor/Google Analytics" -lGoogleAnalyticsServices

(You would need to adjust for your relative paths)

The most helpful thing in getting this sorted out was the Log Navigator (command+8 in Xcode). By putting the static library in the Build Phases Link Binary With Libraries panel and doing a successful build, you can look details of the linker step in the logs, and see how it was passing the Analytics library to the compiler. From there it's just replicating those flags in the xcconfig.