skip to Main Content

On Xcode 12.5 I get this when I try to compile my test target:

/Users/user/Development/project-ios/My ProjectTests/My ProjectTests-Bridging-Header.h:7:9: note: in file included from /Users/user/Development/project-ios/My ProjectTests/My ProjectTests-Bridging-Header.h:7:
#import "SpecHelper.h"
        ^
/Users/user/Development/project-ios/My ProjectTests/SpecHelper.h:36:9: note: in file included from /Users/user/Development/project-ios/My ProjectTests/SpecHelper.h:36:
#import "My_Project-Swift.h"
        ^
/Users/user/Library/Developer/Xcode/DerivedData/My._Project-eybfcywypdtewfbihzpxuwyltrxw/Build/Intermediates.noindex/My Project.build/Debug-iphonesimulator/My Project.build/DerivedSources/My_Project-Swift.h:98:10: error: 'SWIFT_NORETURN' macro redefined
# define SWIFT_NORETURN __attribute__((noreturn))
         ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.5.sdk/usr/lib/swift/shims/Visibility.h:86:9: note: previous definition is here
#define SWIFT_NORETURN __attribute__((__noreturn__))
        ^
<unknown>:0: error: failed to import bridging header '/Users/user/Development/project-ios/My ProjectTests/My ProjectTests-Bridging-Header.h'
Command MergeSwiftModule failed with a nonzero exit code

Project-Swift.h

#if __has_attribute(noreturn)
# define SWIFT_NORETURN __attribute__((noreturn))
#else
# define SWIFT_NORETURN
#endif

and Visibility.h

#if __has_attribute(noreturn)
#define SWIFT_NORETURN __attribute__((__noreturn__))
#else
#define SWIFT_NORETURN
#endif

Note: this works on Xcode 12.4.

Any idea how to work around this?

UPDATE (May 27th): If I delete the derived data, run the test suite it fails with the error described above. Trying to run it the second time seems to work. I have no idea why that is the case.

UPDATE 2: (June 3rd): The problem seems to be that I have a class defined in a .h file that conforms to a protocol defined in a swift file, different targets. This explains why I cannot use forward declaration so I don’t have to import the "MyProject-Swift.h" file.

2

Answers


  1. It’s not immediately clear to me why you’re declaring the macro in different places, but there must be a circular import or something where it ends up getting redefined.

    Option 1 is refactor the code so that you only declare that macro in one spot and then import it from the necessary locations.

    Option 2 is to wrap each declaration so that it doesn’t get redefined:

    #ifndef SWIFT_NORETURN
    
    #if __has_attribute(noreturn)
    #define SWIFT_NORETURN __attribute__((__noreturn__))
    #else
    #define SWIFT_NORETURN
    #endif
    
    #endif
    

    Or:

    #if __has_attribute(noreturn) && !defined(SWIFT_NORETURN)
    #define SWIFT_NORETURN __attribute__((__noreturn__))
    #else
    #define SWIFT_NORETURN
    #endif
    
    Login or Signup to reply.
  2. Try to check, that your header files does not include line #import "Project-Swift.h". Ideally, only *.m files should include this generated header.

    https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search