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').
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.
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.
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.
hi, I have same problem on Apple's sample code : (No member named 'createForest' in 'ForestBuilder::MagicForest' ) with Xcode 15.4 ( works well in Xcode 15.2 )。
have U solve this problem?
Just wanted to add that using Xcode 16 beta 3, the Install Generated Header is YES for all targets, when you install the sample. But the sample still does not compile. Same error.
Checking ForestBuilder-swift.h, I don't see a reference to a memberfunction createForest anywhere, although I do see the enum values (enchanted, etc.).
Does that mean that something is broken?
Edit:
From the ForestBuilder-swift.h file:
// Generated by Apple Swift version 6.0 effective-5.10 (swiftlang-6.0.0.5.15 clang-1600.0.22.6)