# Xcode project with SPM dependencies

**URL:** <https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157>\
**Category:** Using Swift\
**Created:** [November 20, 2018, 11:50pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157 "2018-11-20T23:50:19Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lane\_Schwartz](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lane_schwartz/32/5295_2.png) [@Lane\_Schwartz](https://forums.swift.org/u/Lane_Schwartz)\
**Post date:** [November 20, 2018, 11:50pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/1 "2018-11-20T23:50:19Z")

</div>

Hi,

I'm starting a new Mac app project. This project will rely on an externally developed Swift library (the library itself wraps some C code). The library is set up using SPM.

What is the best practice for creating and setting up an Xcode project that relies on an SPM dependency for a library?

I've tried a couple of things, neither of which have worked.

I first tried creating an Xcode project and then adding the dependent directory. I expected this to work, but I wasn't able to get the Xcode project to have the library as a target.

I next tried using SPM. I copied the various swift and XML files created within Xcode into a new directory, laid out like SPM likes, placing the Mac app source files in a subdir of Sources, and defining a Package.swift file that lists the library as a dependency. I then used SPM to generate an Xcode project file.

Neither of these options worked. Is this even possible right now? I know that Xcode integration with SPM isn't fully mature yet.

Thanks,  
Lane

---

<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:** [November 21, 2018, 7:13am UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/2 "2018-11-21T07:13:32Z")

</div>

As long as there is no first party support in Xcode for that there are only hacks. But it works and I have been using it for quite some time.

You can follow this article and adapt it for macOS: [Adding external libraries to iOS projects with the Swift Package Manager – iOS & Xcode Tutorial](https://www.ralfebert.de/ios-examples/xcode/ios-dependency-management-with-swift-package-manager/)

Basically you create a Swift SPM package that holds all your dependencies inside your macOS project. And you add the generated Xcode project file for this umbrella project to your macOS project as a sub-project.

The main caveat is that you will have to modify the project file holding your dependencies to produce frameworks and modify some other settings. But with some form of script (like a rake-file in the article) this is not too bad.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [November 21, 2018, 2:07pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/3 "2018-11-21T14:07:05Z")

</div>

It’s much easier than that.

1. Create a package to define the top of the dependency tree:  
`$ swift package init`, then customize `Package.swift`

2. Generate an Xcode project from it:  
`$ swift package generate-xcodeproj`

3. Drag that project into the same workspace as your application.

4. For your application target, make sure to select whichever products should be embedded/linked against.

This also works for iOS.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [November 21, 2018, 2:14pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/4 "2018-11-21T14:14:33Z")

</div>

The screenshots in @tkrajacic’s link may be useful to understand steps 3 and 4 above, but you can ignore all of the other stuff about `git`, `gem`, `rake` and any of the other Xcode settings it talks about. None of those have been necessary for some time now.

---

<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 9, 2018, 6:08pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/5 "2018-12-09T18:08:14Z")

</div>

If I need to have a static framework, this still doesn't seem to work.  
Even when I set the type of the library target to `.static` in the Package manifest it produces a dynamic framework when compiled with Xcode. So I have to manually edit the project file.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [December 9, 2018, 7:13pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/6 "2018-12-09T19:13:44Z")

</div>

Correct.

The package manager defaults to static linking for itself, though it can be set to create dynamic libraries (.dylib) instead. However, the Xcode project it produces currently results in frameworks (.framework) instead no matter what (on a per‐module, not per‐library basis, which is also odd). That is fine for most things, but if the distinction is significant to you, consider filing a bug report.

---

<div class="post-metadata">

**Author:** ![kirilltitov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kirilltitov/32/4182_2.png) [@kirilltitov](https://forums.swift.org/u/kirilltitov)\
**Post date:** [December 14, 2018, 2:17pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/7 "2018-12-14T14:17:17Z")

</div>

I can't get it working :( I have a package which uses NIO, but when I added its `xcodeproj` file to my workspace and added it to embedded binaries, it just fails at compile time with error `missing required module 'CNIOAtomics'` (I tried adding it to embedded binaries and frameworks/libraries), no use.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [December 14, 2018, 6:12pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/8 "2018-12-14T18:12:09Z")

</div>

It just created a minimal package to test it, and it worked for me with no errors or warnings what so ever. (swift‐nio 1.12.0, Xcode 10.1)

🤷‍♂️

---

<div class="post-metadata">

**Author:** ![kirilltitov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kirilltitov/32/4182_2.png) [@kirilltitov](https://forums.swift.org/u/kirilltitov)\
**Post date:** [December 14, 2018, 8:43pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/9 "2018-12-14T20:43:20Z")

</div>

Could you pls share it? Maybe I'm doing something wrong...

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [December 14, 2018, 10:01pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/10 "2018-12-14T22:01:10Z")

</div>

In micromanaged, click‐by‐click instructions:

1. In Terminal:

```nohighlight
cd ~/Desktop
mkdir TestPackage
cd TestPackage
swift package init
open Package.swift

```

1. Change `Package.swift` to this:

```swift
// swift-tools-version:4.2
import PackageDescription
let package = Package(
    name: "TestPackage",
    products: [
        .library(name: "TestPackage", targets: ["TestPackage"])
    ],
    dependencies: [
        .package(url: "https://github.com/apple/swift-nio", from: "1.12.0")
    ],
    targets: [
        .target(name: "TestPackage", dependencies: ["NIO"])
    ]
)

```

1. Back in Terminal:

```nohighlight
swift package generate-xcodeproj

```

Then follow these screenshots:

 ![4%20Welcome](https://global.discourse-cdn.com/swift/original/2X/0/065f6b6ed84b9f5e4b3f6ad926e9b138ded3311c.png)  
 ![5%20Template](https://global.discourse-cdn.com/swift/original/2X/c/cfe5e25105618d0465d6b50de97b1636cd765d6b.png)  
 ![6%20Project](https://global.discourse-cdn.com/swift/original/2X/b/b2740626de24ec7d2c3d8eaab4f4afebbe4fe35d.jpeg)  
 ![7%20Location](https://global.discourse-cdn.com/swift/original/2X/4/440001371469d3bdb0e08e77686fdf72e2d4daa3.jpeg)  
 ![8%20Finder](https://global.discourse-cdn.com/swift/original/2X/f/ffca3d437eecb172d6d83e6144f8f3fa3a49e519.png)  
 ![9%20Drag](https://global.discourse-cdn.com/swift/original/2X/1/1e589abfa29e846eb1c23902040f080dec4a993b.png)  
 ![10%20Workspace](https://global.discourse-cdn.com/swift/original/2X/4/435737c7f7a521e471008b7d24cf0c51e40059ad.png)  
 ![11%20Embed](https://global.discourse-cdn.com/swift/original/2X/f/fbc99ac21f22a859b78723e9cb8f12626e22b68d.jpeg)  
 ![12%20Import](https://global.discourse-cdn.com/swift/original/2X/6/67dcc8a7e3a050d8e5d9fc608130ed0912e58448.png)

---

<div class="post-metadata">

**Author:** ![lbdl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lbdl/32/5762_2.png) [@lbdl](https://forums.swift.org/u/lbdl)\
**Post date:** [January 7, 2019, 8:11pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/11 "2019-01-07T20:11:56Z")

</div>

This is a great guide but when I followed it I too got a similar issue, **Missing required module 'xxx'**'  
This seems to relate to Swift wrappers around C Libs.

For future reference in my case I created a wrapper around a C library, this worked fine in its wrapping Package but when I followed this guide and @ [tkrajacic](https://forums.swift.org/u/tkrajacic)'s guide Xcode cannot find the embedded (in the package) C Library.

For this to work for me I had to set Xcode's **Other Swift Flags** as described in the following [StackOverflow](https://stackoverflow.com/questions/43306779/xcode-missing-required-module-error) article.

I had to build the swift package and then set the path to the generated module map

With a file structure as below

 ![43](https://global.discourse-cdn.com/swift/original/2X/a/a3b2ab8f1d74e47b75b166cb7986751a24b19fc3.png)

I had to set the `-xcc` flag as follows:

`-Xcc -fmodule-map-file=$(SRCROOT)/Deps/.build/x86_64-apple-macosx10.10/debug/CTulipIndicators.build/module.modulemap`

and this fixed the missing module error.

Is this poor practise on my part or an issue with SPM and Xcode?

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [January 7, 2019, 10:27pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/12 "2019-01-07T22:27:09Z")

</div>

Just make sure you know the implications if you hard‐code `.../x86_64-apple-macosx10.10/debug/...`. It won’t work for other platforms and it will disrespect any debug/release differences. If the same module map can be found in the product directory somewhere, that would be a better place to point—even if it is nested inside one of the other product bundles.

I’m curious in which layer the issue first appears:

1. Does `$ swift test` work for `Deps/` with a clean build? (i.e. delete `Deps/.build/` before trying) If not, there is an issue with how the package is set up.
2. Does a clean `$ swift package generate-xcodeproj` work for `Deps/`? (i.e. delete `Deps/Deps.xcodeproj` first). Then does the resulting project (`Deps/Deps.xcodeproj` in isolation) pass tests without adjusting anything? If either of these fail, there is a bug in the package manager. Report it at bugs.swift.org.
3. Otherwise it is an issue with Xcode (unless you made a mistake in your workspace set‐up, but it sounds like you know what you are doing). Either way, I would ask the Xcode folks about it.

---

<div class="post-metadata">

**Author:** ![lbdl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lbdl/32/5762_2.png) [@lbdl](https://forums.swift.org/u/lbdl)\
**Post date:** [January 7, 2019, 11:02pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/13 "2019-01-07T23:02:06Z")

</div>

yes I agree it is very brittle and a work around not a fix, I will try your suggestions and report back, I may well have set it all up wrong. Thanks for the response.

---

<div class="post-metadata">

**Author:** ![lbdl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lbdl/32/5762_2.png) [@lbdl](https://forums.swift.org/u/lbdl)\
**Post date:** [January 8, 2019, 3:18pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/14 "2019-01-08T15:18:22Z")

</div>

> [@SDGGiesbrecht](#):
>
> Does `$ swift test` work for `Deps/` with a clean build? (i.e. delete `Deps/.build/` before trying) If not, there is an issue with how the package is set up.

1. This stage works just fine, at the end of the stage we have a `.build` folder with the expected files.

> [@SDGGiesbrecht](#):
>
> Does a clean `$ swift package generate-xcodeproj` work for `Deps/` ? (i.e. delete `Deps/Deps.xcodeproj` first). Then does the resulting project ( `Deps/Deps.xcodeproj` in isolation) pass tests without adjusting anything? If either of these fail, there is a bug in the package manager. Report it at [bugs.swift.org](http://bugs.swift.org).

1. Running a complete clean (ie remove the `.build` folder and the `.xcodeproj` folder) and then `swift package generate-xcodeproj` generates the `.build` folder but no module map or .o files etc. Running the tests from with Xcode, build fine and the tests run.

At this point, having run the build and test phase either via Xcode or SPM we have a `.build` folder.

If we run the command `swift test` then as expected the `.build` folder contains the subfolder `x86_64-apple-macosx10.10` that contains the `debug` or `release` build and the the module map files such as  
`debug/CTulipIndicators.build/module.modulemap` etc, i.e. the missing `module.map` file

If we elide this step i.e just run `swift package generate-xcodeproj` then the module map for the embedded C library resides in `Dependencies.xcodeproj/GeneratedModuleMap/CLibrary/module.map`

This I assume is correct as it makes sense (at least to me)

However when the Dependencies `.xcodeproj` is added to the MacOS application this path seems to be lost.

I have setup the Dependency package as follows:

```swift
import PackageDescription

let package = Package(
    name: "Dependencies",
    products: [
        .library(
            name: "Dependencies",
            targets: ["Dependencies"]),
    ],
    dependencies: [
        .package(url: "https://github.com/lbdl/SwiftTulipIndicators.git", from: "0.1.1"),
    ],
    targets: [
        .target(
            name: "Dependencies",
            dependencies: ["SwiftTulipIndicators"]),
        .testTarget(
            name: "DependenciesTests",
            dependencies: ["Dependencies"]),
    ]
)

```

The package that uses the embedded C library is setup as follows:

```swift
import PackageDescription

let package = Package(
    name: "SwiftTulipIndicators",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "SwiftTulipIndicators",
            targets: ["SwiftTulipIndicators"]),
    ],
    dependencies: [
        .package(url: "https://github.com/lbdl/CTulipIndicatorsPackage.git", from: "0.0.2"),
        .package(url: "https://github.com/Quick/Quick.git", from: "1.3.2"),
        .package(url: "https://github.com/Quick/Nimble.git", from: "7.3.1"),
        ],
    targets: [
        .target(
            name: "SwiftTulipIndicators",
            dependencies: ["CTulipIndicators"]),
        .testTarget(
            name: "SwiftTulipIndicatorsTests",
            dependencies: ["SwiftTulipIndicators", "Quick", "Nimble"]),
    ]
)

```

and finally the wrapped C Library package is as follows:

```swift
import PackageDescription

let package = Package(
    name: "CTulipIndicators",
    products: [
        .library(name: "CTulipIndicators", targets: ["CTulipIndicators"]),
    ],
    targets: [
        .target(
            name: "CTulipIndicators", path: "./Sources/libtulip"),
    ]
)

```

the repos for both are as follows:

[CLibWrapper](https://github.com/lbdl/CTulipIndicatorsPackage)  
[DownstreamWrapper](https://github.com/lbdl/SwiftTulipIndicators)

Perhaps it's down to the way I have setup the packages?

Regardless the error can be fixed with the not ideal `-Xcc` flag as before but this time pointing to the module map contained in the added `Dependencies package project` as follows:

 ![10](https://global.discourse-cdn.com/swift/original/2X/a/a69f5485e2ca673b6f982c7181dbede99e3d5069.png)

with the path to the `GeneratedModuleMap` folder of the Dependency project

some thing like `-Xcc -fmodule-map-file=path/to/dependency/project/DependencyProject.xcodeproj/GeneratedModuleMap/ModuleThatsGoneMissing/module.map`

I'm not sure wether this is an `ID10t` error on my part or an Xcode bug.

This may be useful documentation none the less. Should I put this somewhere else or just file a bug and if so where?

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [January 8, 2019, 9:57pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/15 "2019-01-08T21:57:50Z")

</div>

I tried to follow your steps on my own machine.

1. Directly copying and pasting your “Dependencies” manifest did not work. I had to add `// swift-tools-version:4.2` to the very top. Did you just leave that out when you copied and pasted?

Other than that, the package and the generated “Dependencies” Xcode project both work properly.

The combined workspace runs into its trouble _in the application target_ (meaning all the targets it inherits from the “Dependencies” project still work fine on their own). Since the application does not `import` `CTulipIndicators`, and `SwiftTulipIndicators` has not `@_exported` it, I think this all comes down to the general inconsistencies around `import`. You can read reams on the subject in this thread:

> [@@\_exported and fixing import visibility](https://forums.swift.org/t/exported-and-fixing-import-visibility/9415):
>
> I told @Jon_Shier I'd write something up on @\_exported and visibility in general, so here it is. It's a bit of a brain dump, but I know if I wait until I have time to edit it it won't get posted for months. (That's already true, in fact; I've had this model kicking around my head for probably close to a year now.) So, thanks for the prompt, Jon. 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 i…

Maybe @Aciid knows if there is anything the package manager could do to mitigate it? Maybe there is someplace Xcode would look by default where the Xcode target generated for a C‐dependent package target might put the module map?

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [January 8, 2019, 10:03pm UTC](https://forums.swift.org/t/xcode-project-with-spm-dependencies/18157/16 "2019-01-08T22:03:26Z")

</div>

> [@lbdl](#):
>
> some thing like `-Xcc -fmodule-map-file=path/to/dependency/project/DependencyProject.xcodeproj/GeneratedModuleMap/ModuleThatsGoneMissing/module.map`

Oh and yes, that is the best current workaround. It matches what the package manager generates for the targets it is responsible for.
