Unable to compile Swift Package when used in Xcode


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.

#import "../Defines.h"
#import "Defines.h"

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

cSettings: [

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.

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

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

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.


Im Adding Package.swift to the following library: 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: [

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

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

@NeoNacho I just filed a new bug regarding this issue SR-14011 including an example. The bug still occur on the latest Xcode at the time.

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.

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.

The original bug report was deleted so I created a new one SR-14469

Terms of Service

Privacy Policy

Cookie Policy