# Statically linked Library within a Dynamic Framework. Works, but how, and is it valid?

**URL:** <https://forums.swift.org/t/statically-linked-library-within-a-dynamic-framework-works-but-how-and-is-it-valid/63374>\
**Category:** Using Swift\
**Tags:** dynamic, packagemanager, static, framework, swift-pm\
**Created:** [February 26, 2023, 3:06pm UTC](https://forums.swift.org/t/statically-linked-library-within-a-dynamic-framework-works-but-how-and-is-it-valid/63374 "2023-02-26T15:06:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![asavill](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/asavill/32/28908_2.png) [@asavill](https://forums.swift.org/u/asavill)\
**Post date:** [February 26, 2023, 3:06pm UTC](https://forums.swift.org/t/statically-linked-library-within-a-dynamic-framework-works-but-how-and-is-it-valid/63374/1 "2023-02-26T15:06:54Z")

</div>

I'm relatively new to Swift and building software for Apple platforms. So far I'm enjoying it, but one of the things that struck me from the beginning was leaky dependencies/imports.

This is something that has been discussed extensively in the community, and some solid proposals have been tabled, kick-started by @jrose on the @\_implementationOnly work.

A snippet from the 'exported-and-fixing-import-visibility' post to summarise the problem

> Today, if your Swift library "Foo" uses a library "Bar", the headers (or swiftmodule files) of "Bar" must be available to all clients of "Foo". This is clearly undesirable, especially when "Bar" is just an implementation detail. (It may even be linked statically.)

More literature on the topic here:

- [@\_exported and fixing import visibility](https://forums.swift.org/t/exported-and-fixing-import-visibility/9415) (first post)
- [Update on implementation-only imports](https://forums.swift.org/t/update-on-implementation-only-imports/26996) (update on the prototype solution)
- [[Pre-Pitch] Import access control: a modest proposal](https://forums.swift.org/t/pre-pitch-import-access-control-a-modest-proposal/50087) (latest pitch and state of things now)

Another forum post also highlights a setup very similar to the one below and illustrates the problem,

> [@Compiling a dynamic framework with a statically linked library creates dependencies in ".swiftmodule" file](https://forums.swift.org/t/compiling-a-dynamic-framework-with-a-statically-linked-library-creates-dependencies-in-swiftmodule-file/22708):
>
> Steps to reproduce: Create a new project in Xcode with template Cocoa Touch Framework, and name it TestFramework. Add another target in this project with template Cocoa Touch Static Library, and name it TestLibrary. In the auto-generated file TestLibrary.swift, add a public interface like: public struct TestLibrary { public init() { } public let name = "static library" } In target TestFramework, link libTestLibrary.a. In target TestFramework, create a new file with t…

As the above post states (and my early understanding assumes) If we have a setup like so:

Static library named `InternalLibrary`. `InternalLibrary` is statically linked into a framework called `FrameworkA`.

An iOS App Target embeds and links `FrameworkA`.

At this point, most of the literature I've read about this states that the App Target would now also need to link to `InternalLibrary`. This is exactly what @ylorn experiences in [their post.](https://forums.swift.org/t/compiling-a-dynamic-framework-with-a-statically-linked-library-creates-dependencies-in-swiftmodule-file/22708). This makes sense as to why proposals like @\_implementationOnly (and perhaps an internal import) exist. For example, If `InternalLibrary` was brought in with `@_implementationOnly` inside `FrameworkA`, then it would make sense for the App Target to **not** require an explicit link to InternalLibrary.

I believe I have a good enough grasp of this. However, when the above setup is tried with Xcode 14.1 with the iOS 16.1 App Target, I can omit the need to link to `InternalLibrary` in the App Target. This works if `InternalLibrary` is either a **Cocoa Touch Static Library** or a **Swift Package which produces a static library**. This seems to go against my understanding and a lot of the discussion around this issue.

To expand a little more, InternalLibrary is still found on the public interface of `FrameworkA`. And the App Target can still import and use `InternalLibrary`, so a solution like @\_implementationOnly would be better, but my question is, how does the above solution work? And is it a bad idea?

Note: I've build and run on simulator and device.
