# Unable to compile Swift Package when used in Xcode

**URL:** <https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081>\
**Category:** Using Swift\
**Tags:** objective-c, swift, xcode, packagemanager\
**Created:** [May 2, 2020, 1:38pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081 "2020-05-02T13:38:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![karhm](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/karhm/32/5099_2.png) [@karhm](https://forums.swift.org/u/karhm)\
**Post date:** [May 2, 2020, 1:38pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/1 "2020-05-02T13:38:20Z")

</div>

Hello

I have a library written in Objective-C that I'm trying to add a **Package.swift** to it.  
When I add this library as a Swift package to other projects (written in Swift or Objective-C) using Xcode I get an error because the **SWIFT\_PACKAGE** flag is not defined, this flag is used to conditionally import some headers.

```
#if SWIFT_PACKAGE
#import "../Defines.h"
#else
#import "Defines.h"
#endif // SWIFT_PACKAGE

```

I tried adding the flag to **Package.swift** but this did not solve the issue.

```
cSettings: [
    .define("SWIFT_PACKAGE")
]

```

After some debugging I found this in Xcode 11.4 (11E146) where the **SWIFT\_PACKAGE** flag is defined and not defined at the same time.

 ![Xcode](https://global.discourse-cdn.com/swift/original/2X/b/bd68947bb6af466fcbaf715be90b5de5e9c09305.png)

When I try to build the project using swift build or using the Xcode project everything work fine.

---

<div class="post-metadata">

**Author:** ![NeoNacho](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/neonacho/32/1368_2.png) [@NeoNacho](https://forums.swift.org/u/NeoNacho)\
**Post date:** [May 4, 2020, 9:11pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/2 "2020-05-04T21:11:37Z")

</div>

This seems concerning, but I wasn't able to reproduce it. Could you please file a bug for this including your example?

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [May 4, 2020, 9:14pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/4 "2020-05-04T21:14:39Z")

</div>

This may be a difference in C's `#if` vs. `#ifdef`, given how you've manually defined `SWIFT_PACKAGE`. Using `#ifdef` should give you consistent behavior.

---

<div class="post-metadata">

**Author:** ![karhm](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/karhm/32/5099_2.png) [@karhm](https://forums.swift.org/u/karhm)\
**Post date:** [May 4, 2020, 9:47pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/5 "2020-05-04T21:47:49Z")

</div>

@Jon_Shier

 ![Swift](https://global.discourse-cdn.com/swift/original/2X/3/38aeaca05b81d25ab1235bae2c491d6b94993f4a.png)

---

<div class="post-metadata">

**Author:** ![karhm](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/karhm/32/5099_2.png) [@karhm](https://forums.swift.org/u/karhm)\
**Post date:** [May 4, 2020, 9:54pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/6 "2020-05-04T21:54:29Z")

</div>

Im Adding _Package.swift_ to the following library: **[https://github.com/karimhm/google-api-objectivec-client-for-rest](https://github.com/karimhm/google-api-objectivec-client-for-rest)**.  
Add it to any Objective-C or Swift project and choose ~~**swift-pm**~~ **swift-pm-issue** branch.

The double definition problem occur when adding this flag to _Package.swift_:

```
cSettings: [
    .define("SWIFT_PACKAGE")
]

```

Otherwise _SWIFT\_PACKAGE_ does not seem to be defined which is odd given that this is a Swift Package.

---

<div class="post-metadata">

**Author:** ![karhm](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/karhm/32/5099_2.png) [@karhm](https://forums.swift.org/u/karhm)\
**Post date:** [May 6, 2020, 5:29pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/7 "2020-05-06T17:29:33Z")

</div>

The issue only happen inside `.h` header files and nothing else. Not inside `Objective-C/C++` source files or `C++` header or source files.

---

<div class="post-metadata">

**Author:** ![karhm](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/karhm/32/5099_2.png) [@karhm](https://forums.swift.org/u/karhm)\
**Post date:** [January 1, 2021, 11:06am UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/8 "2021-01-01T11:06:51Z")

</div>

@NeoNacho I just filed a new bug regarding this issue [SR-14011](https://bugs.swift.org/browse/SR-14011) including an example. The bug still occur on the latest Xcode at the time.

---

<div class="post-metadata">

**Author:** ![paulb777](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulb777/32/7986_2.png) [@paulb777](https://forums.swift.org/u/paulb777)\
**Post date:** [April 6, 2021, 9:34pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/9 "2021-04-06T21:34:13Z")

</div>

I've seen that `SWIFT_PACKAGE` is not enabled when Xcode constructs modules from public headers. I've restructured headers to avoid the need for differentiating between SPM and CocoaPods clients. Details [here](https://github.com/firebase/firebase-ios-sdk/blob/master/HeadersImports.md#header-file-types-and-locations---for-header-file-creators).

---

<div class="post-metadata">

**Author:** ![NeoNacho](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/neonacho/32/1368_2.png) [@NeoNacho](https://forums.swift.org/u/NeoNacho)\
**Post date:** [April 6, 2021, 10:03pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/10 "2021-04-06T22:03:26Z")

</div>

Good point, I think that requires some thinking on how it should work in that context. Since the client of a package is not necessarily a package, it probably should not get the `SWIFT_PACKAGE` define. We could impart the setting on clients, but there isn't really a way to restrict it to individual modules/headers, so it would affect any headers that are imported by the clients, also ones from non-packages.

---

<div class="post-metadata">

**Author:** ![karhm](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/karhm/32/5099_2.png) [@karhm](https://forums.swift.org/u/karhm)\
**Post date:** [April 10, 2021, 4:45pm UTC](https://forums.swift.org/t/unable-to-compile-swift-package-when-used-in-xcode/36081/11 "2021-04-10T16:45:13Z")

</div>

The original bug report was deleted so I created a new one [SR-14469](https://bugs.swift.org/browse/SR-14469)
