# How to link a Swift Package as dynamic

**URL:** <https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062>\
**Category:** Using Swift\
**Tags:** xcode, packagemanager\
**Created:** [December 27, 2019, 8:43am UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062 "2019-12-27T08:43:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcifani](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pcifani/32/10014_2.png) [@pcifani](https://forums.swift.org/u/pcifani)\
**Post date:** [December 27, 2019, 8:43am UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/1 "2019-12-27T08:43:16Z")

</div>

According to the SwiftPM documentation, when defining the type of a package:

> `Leave this parameter unspecified to let to let the Swift Package Manager choose between static or dynamic linking (recommended).`

However, when using such libraries from Xcode in a fairly simple setup and running unit tests, Xcode complains about duplicated symbols:

```swift
objc[97739]: Class _TtC8Deferred18POSIXReadWriteLock is implemented in both /Users/pcifani/Library/Developer/CoreSimulator/Devices/835DF32C-44B3-4841-B6CB-A7B2D12744D7/data/Containers/Bundle/Application/C5053FF1-BF0D-434A-94C6-BAAE5FF414F0/VideoAsk.app/Frameworks/BSWFoundation.framework/BSWFoundation (0x10825ec48) and /Users/pcifani/Library/Developer/CoreSimulator/Devices/835DF32C-44B3-4841-B6CB-A7B2D12744D7/data/Containers/Bundle/Application/C5053FF1-BF0D-434A-94C6-BAAE5FF414F0/VideoAsk.app/VideoAsk (0x106c31310). One of the two will be used. Which one is undefined.

```

The app's dependency tree looks like this: libFoo depends on libBar and App depends on both libFoo and libBar, and the Tests bundle for the App depends on both the app and the library.

I can't find any option in Xcode's UI to "force" the libraries to be dynamic, and SwiftPM seems to not see this issue when linking the packages and choosing a dynamic configuration for me.

Any workaround except forking the library and setting all the types to .dynamic?

---

<div class="post-metadata">

**Author:** ![tkrajacic](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkrajacic/32/1173_2.png) [@tkrajacic](https://forums.swift.org/u/tkrajacic)\
**Post date:** [December 27, 2019, 9:51am UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/2 "2019-12-27T09:51:09Z")

</div>

Yeah, that's one of the most annoying remaining problems with SPM in Xcode. SPM always chooses static libraries…

What you can do as a workaround is to create a new shim package (can live right inside your app's repo) which you explicitly mark as dynamic, and then have its manifest specify all the packages you need to share. Then your app and other targets previously using those, must only link to your dynamic package. This sucks for so many reasons, but is the only way I know how to fix this.

---

<div class="post-metadata">

**Author:** ![pcifani](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pcifani/32/10014_2.png) [@pcifani](https://forums.swift.org/u/pcifani)\
**Post date:** [December 27, 2019, 10:27am UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/3 "2019-12-27T10:27:34Z")

</div>

Yeah, that is a workaround I've been using, but as you mention, it sucks for a lot of reasons...

---

<div class="post-metadata">

**Author:** ![crabman448](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crabman448/32/16636_2.png) [@crabman448](https://forums.swift.org/u/crabman448)\
**Post date:** [March 3, 2020, 12:05pm UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/4 "2020-03-03T12:05:14Z")

</div>

Does someone have any updates regarding this topic?

---

<div class="post-metadata">

**Author:** ![pcifani](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pcifani/32/10014_2.png) [@pcifani](https://forums.swift.org/u/pcifani)\
**Post date:** [March 3, 2020, 1:31pm UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/5 "2020-03-03T13:31:27Z")

</div>

No news here... :(

---

<div class="post-metadata">

**Author:** ![TheDeanosaurus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thedeanosaurus/32/9098_2.png) [@TheDeanosaurus](https://forums.swift.org/u/TheDeanosaurus)\
**Post date:** [March 3, 2020, 3:54pm UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/6 "2020-03-03T15:54:35Z")

</div>

Do you mind elaborating a bit on the "shim" package? I'm guessing it would be one dynamic library package at the root (does it have a target?) with everything else added as dependencies? Did you have to turn off code coverage? Do you still import by individual target or just `import MyDynamicLibraryHoldingEverything`?

---

<div class="post-metadata">

**Author:** ![pcifani](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pcifani/32/10014_2.png) [@pcifani](https://forums.swift.org/u/pcifani)\
**Post date:** [March 3, 2020, 3:57pm UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/7 "2020-03-03T15:57:34Z")

</div>

I just created a new Framework using Xcode (called FacadeKit on the screenshot here below) and added all required Packages as dependencies. After that, you embed that into your .app and Unit Test target. After that the `import SomeStaticPackage` should all work without ever importing the shim framework on your code

 ![image](https://global.discourse-cdn.com/swift/original/2X/c/cc8056f2781b03117c70d39a126404a157ac171e.jpeg)

---

<div class="post-metadata">

**Author:** ![TheDeanosaurus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thedeanosaurus/32/9098_2.png) [@TheDeanosaurus](https://forums.swift.org/u/TheDeanosaurus)\
**Post date:** [March 3, 2020, 4:54pm UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/8 "2020-03-03T16:54:05Z")

</div>

So in my case I've got a bunch of other embedded framework and static library targets within the project, is it safe to just link that embedded framework everywhere it's needed since it's dynamic and will be resolved later?

---

<div class="post-metadata">

**Author:** ![pcifani](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pcifani/32/10014_2.png) [@pcifani](https://forums.swift.org/u/pcifani)\
**Post date:** [March 3, 2020, 5:11pm UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/9 "2020-03-03T17:11:54Z")

</div>

It should be fine, yes. Double check for dylib errors when running the app or running your tests.

---

<div class="post-metadata">

**Author:** ![renaudjenny](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/renaudjenny/32/11138_2.png) [@renaudjenny](https://forums.swift.org/u/renaudjenny)\
**Post date:** [April 6, 2020, 4:48pm UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/10 "2020-04-06T16:48:43Z")

</div>

For folks who interested in a step by step for that I created one: [GitHub - renaudjenny/Swift-Package-Manager-Static-Dynamic-Xcode-Bug: Workaround about SPM (Swift package manager) deal with Xcode 11.4 and Swift 5.2 with external static libraries. Adding an internal dynamic library to resolve static code duplication error](https://github.com/renaudjenny/Swift-Package-Manager-Static-Dynamic-Xcode-Bug)

---

<div class="post-metadata">

**Author:** ![pcifani](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pcifani/32/10014_2.png) [@pcifani](https://forums.swift.org/u/pcifani)\
**Post date:** [April 7, 2020, 7:56am UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/11 "2020-04-07T07:56:37Z")

</div>

Hey [renaudjenny](https://forums.swift.org/u/renaudjenny), thanks a lot for the nice writeup!

---

<div class="post-metadata">

**Author:** ![1oo7](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/1oo7/32/17598_2.png) [@1oo7](https://forums.swift.org/u/1oo7)\
**Post date:** [January 14, 2021, 7:42am UTC](https://forums.swift.org/t/how-to-link-a-swift-package-as-dynamic/32062/12 "2021-01-14T07:42:21Z")

</div>

I discovered that Xcode builds .frameworks for all your packages. They'll live in $(BUILT\_PRODUCTS\_DIR)/PackageFrameworks. You can drag those to your app, and as long as they exist before you try to build the app itself, then the compiler will be happy.

Not a great workaround but hey.
