How to avoid linking system library only when building for iOS?

So I'm running into a bit of a tricky situation including a system library with SPM.

Basically, I want to include GLFW as a dependency only when building for macOS or linux, but to exclude it when building for iOS, because the compilation fails when linking.

What I have tried so far is to include GLFW as a dependency, and then conditionally skip the import statements when building for iOS. So in other words, my Package.swift looks like this:

let package = Package(
    name: "Foo",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
            name: "Foo",
            targets: ["Foo"]
    targets: [
            name: "Foo",
            dependencies: [
            name: "CGLFW3",
            pkgConfig: "glfw3",
            providers: [

And then in my code, I always do imports like this:

#if !os(iOS)
import CGLFW3

However, when building for simulator, I still encounter this error:

ld: building for iOS Simulator, but linking in dylib built for macOS, file '/opt/homebrew/Cellar/glfw/3.3.7/lib/libglfw.dylib' for architecture arm64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

So I assume just having the dependency declared in Package.swift is enough for SPM to attempt to link the library.

Is there any way to avoid this?

I think you want to write the target dependency like this:

.target(name: "Foo",
        condition: .when(platforms: [.macOS, .linux])),

... and then keep the #if in the code as well.

I haven't had exactly your situation but do something similar in a package to link dependency A only on macOS and dependency B only on linux etc.

Yes this is exactly what I was looking for, thank you!