Unable to compile Swift Package when used in Xcode

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.

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.

@Jon_Shier

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: [
    .define("SWIFT_PACKAGE")
]

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.

Terms of Service

Privacy Policy

Cookie Policy