Use Swift framework code in C++ app - missing -Swift.h header

macOS Sonoma 14.4
Xcode 15.3

Hi,

I'm experimenting with C++/Swift interop and am following the official documentation, especially the section "Using Swift APIs of Imported Framework Target".
I could call Swift code from C++ when both Swift and C++ source files belong to the same app bundle or framework target, by importing the -Swift.h header.
However, I'm not able to import the Swift code from a framework using a different C++ target.
This is my test project setup:

testApp is my app bundle and subprocesses is my framework, containing the auto- generated and unchanged subprocesses.h and some example swift code with a single public function. The subprocesses framework is added as a dependency to testApp and the framework has the C++ interoperability enabled.

But when I try to import the auto-generated -Swift.h in main.cpp, it doesn't show up.

What do I need to do so that I can call Swift framework code in a different C++ target? Could the calling target also be command line app or does it have to be an app bundle? Thanks!

Addendum

I've also experimented with Apple's Xcode example projects. The "Mixing Languages in an Xcode project" (Link) works as expected. I was able to add a command line app target, and when I add the Fibonacci framework as a dependency, I'm able to use #include <Fibonacci/Fibonacci-Swift.h> and access the Swift API.

However, the second of Apple's examples, "Calling APIs Across Language Boundaries" (Link) fails to compile out of the box (No member named 'createForest' in 'ForestBuilder::MagicForest').

1 Like

I think I found the problem. It's the build setting Install Generated Header under the Swift Compiler - General sections of the framework's build settings. Xcode 15.3 sets this to No. You need to set this manually to Yes so that the generated -Swift.h header will be put in the frameworks Headers directory from which it can then be found for importing in other targets.
This needs to be added to the documentation.

Good discovery of this setting ! After reading your solution, I look at the Xcode 15.3 doc and discover the following at Build settings reference | Apple Developer Documentation

Install Generated Header
Setting name: SWIFT_INSTALL_OBJC_HEADER
For frameworks, install the C++/Objective-C generated header describing bridged Swift types into the PUBLIC_HEADERS_FOLDER_PATH so they may be accessed from Objective-C or C++ code using the framework. Defaults to YES.

About the error "No member named.." I also encounter in several cases using Swift both the current version and nightly builds. The Cxx interop while very good advancements is still a work in progress.

Hooping for good news for Swift 6 at WWDC 2024 !

1 Like

It's worth noting that the build settings reference says that the setting defaults to Yes (which is kind of true), but a framework target overwrites this by default to No.

I see, yes this operation of overwrite on a target is confusing and should be documented

@wAuner I change "Install Generated Header" manually to YES, but still get this error on Xcode 15.3
Does I miss something ?

Impossible to say if you don‘t provide more information. Did you add the framework as a dependency in the consuming target?