I'm trying to create a C++ object in a C++ file and pass it as an argument to a Swift method called from C++, so Swift can call back into C++ using that object. But I get a link error:
ld: Undefined symbols:
type metadata accessor for __C.CallMeClass, referenced from:
swift::TypeMetadataTrait<CallMeClass>::getTypeMetadata() in CallMeClass.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My function in C++ to kick it all off is:
#include "CallMeClass.hpp"
#include "SwiftCPlusPlus-Swift.h"
void DoSomething() {
CallMeClass cmc(7);
auto swiftObject = SwiftCPlusPlus::SwiftClass::init(42, "Hello");
swiftObject.doSomething(cmc);
}
and my Swift class:
import Foundation
public struct SwiftClass {
public init(num1: Int, text: String) {
self.num1 = num1
self.text = text
}
public func doSomething(cmc: CallMeClass) {
print("\(self) in Swift got \(num1), \"\(text)\"")
cmc.CallBack()
}
var num1: Int
var text: String
}
and the callback class header:
#pragma once
#include <swift/bridging>
class CallMeClass {
public:
CallMeClass(int foo);
CallMeClass(const CallMeClass& original);
~CallMeClass();
void CallBack() const;
protected:
int mFoo;
};
Implementation:
#include "CallMeClass.hpp"
#include "SwiftCPlusPlus-Swift.h"
#include <iostream>
CallMeClass::CallMeClass(int foo) : mFoo(foo) {
}
CallMeClass::CallMeClass(const CallMeClass& original) : mFoo(original.mFoo) {
}
CallMeClass::~CallMeClass() {
}
void CallMeClass::CallBack() const {
std::cout << "Call back!" << std::endl;
}
Bridging header:
#include "CallMeClass.hpp"
Anyone have an idea what I'm doing wrong?
PS - I found Undeclared use of identifier in generated Module-Swift file but the fix in there doesn't work for me, I'm already including the files in that order.
Update: Added bridging header and swift/bridging
include.