In my experiments with the cxx-interop and the latest toolchain (January 28) I tried to expose a Swift function to c++. I found the @_expose attribute is available in this release and compiled an ultra simple Swift library named "MySwiftLib":
// swift-tools-version: 5.7
import PackageDescription
let package = Package(
name: "SwiftCXXInteropExample",
platforms: [
.macOS(.v12),
],
products: [
.library(name: "MySwiftLib", type: .dynamic, targets: ["MySwiftLib"])
],
dependencies: [],
targets: [
.target(name: "MySwiftLib", swiftSettings: [.unsafeFlags(["-enable-experimental-cxx-interop"])])
]
)
// Ultra simple Swift library
// file.swift
import Foundation
@_expose(Cxx, "my_exposed_swift_add")
func my_swift_add(a: Int64, b: Int64) -> Int64 {
return a + b
}
The swift build ran ok and I expected to find the "my_swift_add" function signature in the generated MySwiftLib-Swift.h header and the corresponding c++ symbol in the libMySwiftLib.dylib.
The MySwiftLib-Swift.h contain the library namespace MySwiftLib but not the function signature.
namespace MySwiftLib __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("MySwiftLib") {
} // namespace MySwiftLib
The dylib seems to include only the Swift function mangled signature.
admin@mbam1 debug % nm -gU libMySwiftlib.dylib
0000000000003118 T _$s10MySwiftLib12my_swift_add1a1bs5Int64VAF_AFtF
admin@mbam1
Whatever I use the @_expose attribute or not make no difference.
Is I miss something ?
Thanks