Using Swift class from C++ Undefined symbols "_swift_release" and "_swift_retain"

I suppose I have to link my C++ code with the Swift runtime to resolve the "_swift_release" and "_swift_retain" symbols.

I need to add a flag when building the Swift lib for the Swift lib include the runtime ? Or a way to link the Swift runtime from C++ ?

I am testing with the Feb 01 5.8 snapshot on macOS 13.2, MacBook Air M1.

admin@mbam1 CxxToSwift % xcrun --toolchain "Swift 5.8 Development Snapshot" clang++ main.cpp -lMySwiftLib
Undefined symbols for architecture arm64:
  "_swift_release", referenced from:
      swift::_impl::RefCountedClass::~RefCountedClass() in main-bdab04.o
  "_swift_retain", referenced from:
      swift::_impl::RefCountedClass::RefCountedClass(swift::_impl::RefCountedClass const&) in main-bdab04.o
ld: symbol(s) not found for architecture arm64
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)

I find the required runtime libswiftCore.dylib in the toolchain. I just linked the libswiftCore.dylib with the c++ test code and now I can use the Swift class from C++. So far so good.

1 Like

Yes, you need to link with the Swift standard library.