SwiftPM Binary Linker Questions

I've made my first SwiftPM binary target in https://github.com/firebase/firebase-ios-sdk/pull/6215.

I was able to successfully run an Objective C client app, but only after adding -ObjC to Other Linker Options to resolve runtime crashes from categories being stripped.

Is there a way to specify that the library requires the app to link it with -ObjC?

My Swift client app fails to link with missing StoreKit framework and sqlite library symbols. CocoaPods has frameworks and libraries podspec option to specify required system frameworks and libraries. Is there anything comparable for SwiftPM.

If not, what is the best practice? One approach might be to add references to the needed libraries to a wrapper source target?

1 Like

There's linkerSettings (https://developer.apple.com/documentation/swift_packages/target/3112791-linkersettings) for the frameworks/libraries part -- you would need to apply them to "FirebaseAnalyticsWrapper", though.

We don't have a way to specify -ObjC today, but it sounds like we should. It might be time for us to collect the feedback we got since https://github.com/apple/swift-evolution/blob/master/proposals/0238-package-manager-build-settings.md and run a small update proposal to add some requested settings.

2 Likes

Thanks, but adding linkerSettings doesn't seem to make a difference. I added

  linkerSettings: [
    .linkedLibrary("sqlite3"),
    .linkedLibrary("c++"),
    .linkedLibrary("z"),
    .linkedFramework("StoreKit"),
  ]

but still see:

Undefined symbols for architecture x86_64:
"OBJC_CLASS$_SKProductsRequest", referenced from:
objc-class-ref in GoogleAppMeasurement(APMProductsRequest_ed0c0ff930f5baa9c26583789c418f16.o)
"_sqlite3_errmsg", referenced from:
-[APMSqliteStore lastErrorMessage] in GoogleAppMeasurement(APMSqliteStore_acc99dfbcd8c054676308fb24061e3f7.o)
"_sqlite3_bind_blob", referenced from:
___47-[APMSqliteStore bindValues:toStatement:error:]_block_invoke in

Am I missing something else? I notice the docs specify linkLibrary instead of linkedLibrary, but that doesn't seem available in Xcode12b3.

I'm able to workaround by explicitly adding the libraries and frameworks to the app via the Link Binary With Libraries Build Phase.

There is a bug in Xcode 12 before beta 4 where SPM linker flags might be ignored. It seems to be fixed in beta 4. We saw the same thing trying to link CFNetwork for Alamofire.

1 Like

Also keep in mind that your code, when compiled outside of Xcode, is treated as Objective-C++: Undefined C++ symbols in pure Objective-C library target

Confirmed that Xcode 12 beta 4 fixes the linker flags issue.

Should I file a bug for the -ObjC issue?

Sounds like it.

https://bugs.swift.org/browse/SR-13358

Terms of Service

Privacy Policy

Cookie Policy