How to fix 'linking against a dylib which is not safe for use in application extensions' warning

May 18, 2022 · Follow on Twitter and Mastodon xcodespm

Swift packages is a powerful tool for separating your code into modules. However, Xcode currently shows a linking against a dylib which is not safe for use in application extensions warning when linking packages in certain ways, even when a package is safe.

Let’s consider a test app that’s called MyApp, that has a main app target, a MyAppWidget widget extension target and a MyAppFramework framework target:

Xcode project with an app, a widget and a framework target

Let’s create a new package called MyPackage in a sub folder and add it as a local package. The new package has no logic and doesn’t depend on any extension unsafe api:s.

Xcode project with a local package

Since the package doesn’t use any unsafe api:s, we can link our widget extension to it and build the project without getting any errors or warnings:

Xcode widget with a linked local package

We can even check “Allow app extension API only” in our framework, link it to the package and build the project without getting any errors or warnings:

Xcode framework with a linked local package

We can also link the main app target or the widget to either MyAppFramework or MyPackage without getting any errors or warnings:

Xcode app with linked framework

However, if we link the app or widget to both MyAppFramework and MyPackage, we get a linking against a dylib which is not safe for use in application extensions warning:

Xcode app with linked framework and package

Since this warning is incorrect for our package, I’d like to remove it to avoid bloating the project with incorrect or irrelevant warnings. We should only have warnings that can be acted on, and aim to fix all warnings.

To suppress the warning, you can add $(inherited) -Xlinker -no_application_extension to “other linker flags” for any target that causes this warning.

In this case, we can add it to the framework and widget to make this warning go away:

Framework with other linker flags

However, this is not recommended, since the warning may be valid. We must not suppress warnings for things that can actually break your app.

Another alternative is to add this information to the package itself, instead of suppressing the warning in the project. You can do this by specifying linkerSettings for the package:

linkerSettings: [.unsafeFlags(["-Xlinker", "-no_application_extension"])])

This will make the package file look something like this:

Package with linker settings

This will make the warnings go away, which means that you don’t have to suppress them in the project. Just make sure your package is actually safe for extensions before you do this.

Discussions & More

Please share any ideas, feedback or comments you may have in the Disqus section below, or by replying to this tweet

If you found this text interesting, make sure to follow me on Twitter and Mastodon for more content like this, and to be notified when new content is published.

If you like & want to support my work, please consider sponsoring me on GitHub Sponsors.