Importing Swift libraries from Objective C++

How can you import a Swift target built with Swift Package Manager from an Objective C++ source?

Based on Apple Developer Documentation, #import <module/module-Swift.h> works in CocoaPods, but not in Swift Package Manager.

Any suggestions and/or examples about using Swift language Swift package from Objective C++?

2 Likes

SPM doesn't support generating the header. If the API is sufficiently simple you can write the header yourself (e.g. in our tests we define a class in Swift and then manually declare the bits we need to use from objc). If the target is too complicated for this but supports a non-SPM build, you could build it with that and then copy the generated header to your source tree and import that (and then remember to keep it up-to-date when you update the library).

1 Like

Hi @tgoyne! Can I please ask for some clarification on "SPM doesn't support generating the header"?

In my small test package containing only Swift, when building I see logs that indicate that the "MyModule-Swift.h" and "MyModule.modulemap" have been created. (My specific problem is that I can't figure out the path of this header in an ObjC++ client that consumes the package.)

I was able to expose the autogenerated Swift headers of my Swift packages by adding $GENERATED_MODULEMAP_DIR to my header search paths.