I have c++ macOs app(Xcode +14) and I try to add call to swift code. I can't find any simple c++ xcodeproj call to swift code. I create new simple project and fail to build it with error when I try to include #include <SwiftMixTester/SwiftMixTester-Swift.h>: main.m:9:10: error: 'SwiftMixTester/SwiftMixTester-Swift.h' file not found (in target 'CppCallSwift' from project 'CppCallSwift') note: Did not find header 'SwiftMixTester-Swift.h' in framework 'SwiftMixTester' (loaded from '/Users/yanivsmacm4/Library/Developer/Xcode/DerivedData/CppCallSwift-exdxjvwdcczqntbkksebulvfdolq/Build/Products/Debug') .
Please help.
I’m not sure what’s going on in your case, but I’ve been meaning to play around with C++ interoperability for a while so I sat down to test this today. I started out by testing the case where Swift calls C++:
-
In Xcode 26.0.1, I created a new project from the macOS > Command Line template, selecting Swift as the language.
-
I changed the C++ and Objective-C Interoperability build setting to C++ / Objective-C++.
-
I created a new file and its header from the macOS > C++ File template.
-
That asked me whether I want to create a bridging header, which I agreed to.
-
In that bridging header I added:
#include "CppSide.hpp" -
In
CppSide.hppI added a prototype of a function:extern int cppAdd(int a, int b);and in
CppSide.cppI added the implementation:extern int cppAdd(int a, int b) { return a + b; } -
In
main.swift, I added code to call that:let c = cppAdd(40, 2) print("Hello, World! \(42)") -
I built and ran the tool. It printed:
Hello, World! 42
Next I testing the other direction:
-
I created a new file from the macOS > Swift template.
-
I populated it like so:
public class SwiftSide { public static func swiftAdd(_ a: CInt, _ b: CInt) -> CInt { return a + b } } -
In
CppSide.cpp, I added an include of the Swift header:#include "Test805433-Swift.h"then modified
cppAdd(…)to call Swift:extern int cppAdd(int a, int b) { return Test805433::SwiftSide::swiftAdd(a, b); }Note that
Test805433is the C++ namespace associated with my Swift module, and because this is a command-line tool the module name is the name of my tool target. -
I re-ran the tool and it produced the same result.
Nice!
Please repeat these steps at your end and see if I can make it work. If so, you have a working example that you can crib from for your real project.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"