Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Created November 14, 2022 13:56
Show Gist options
  • Save mbrandonw/8168451acd08bcde6e970422e2f359c2 to your computer and use it in GitHub Desktop.
Save mbrandonw/8168451acd08bcde6e970422e2f359c2 to your computer and use it in GitHub Desktop.

Automatically add -ObjC when linking static libraries that contain Swift or ObjC code

FB11788325

Is it possible for Xcode/linker to automatically link -ObjC when linking static libraries that contain Swift or ObjC code?

Without that it is possible for a protocol conformance in a static library to be seemingly stripped from the library. For example, if you have the following protocol hierarchy in a static library:

protocol P {}
protocol Q: P {}

And you create a conformance to P and Q in separate files:

// ConformanceP.swift
struct Conformance: P {}

// ConformanceQ.swift
extension Conformance: Q {}

Then, in a separate library, checking for a dynamic conformance fails, but a direct check succeeds:

func isAnyQ(_ p: any P) -> Bool {
  p is any Q
}
func checkConformanceIndirectly() -> Bool {
  isAnyQ(Conformance())
}
checkConformanceIndirectly() // false
Conformance() is any Q       // true

Once can fix this by adding -ObjC to the linker flags of the application.

I previously thought this was a bug and filed a feedback (see: FB11779019), but then was told this is the expected behavior (see here).

We have had a few people run into this problem in one of our open source libraries. See here for an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment