VisionOS Support for Mixing Swift and C++ Interoperability

Hello Apple Developer Forum community,

I'm exploring the possibilities of mixing Swift and C++ for a project that involves VisionOS. The Swift official documentation on Mixing Swift and C++ mentions that C++ interoperability is supported for development and deployment on all platforms that Swift supports.

I would like to confirm whether this support extends to VisionOS. Does VisionOS allow the use of C++ APIs, and how can various C++ APIs be effectively imported and used in Swift for VisionOS development?

If anyone has experience or insights related to mixing Swift and C++ specifically for VisionOS, I would greatly appreciate your guidance and advice.

Thank you for your assistance.

Best regards,

Answered by jeymichael in 769408022

Mixing swift & C++ works for visionOS. I am able to pass std:vector<int>, std:vector<float> as arguments to C++ methods successfully, for visionOS targets.

But I suspect there are some gaps/bugs, in visionOS support. Or, maybe some compiler settings/includes is creating some confusion.

Because I can see the problem you mention, in my builds as well.

The line let swiftString = String(cxxString) throws compiler error No exact matches in call to initializer.

The line let cxxString = std.string("This is a C++ string") causes runtime error, silently creating a zero length string.

Yes.

thanks @Izhido_XR for the prompt response.

May be I am doing something wrong, but the below code snipper from the official swift doc is working fine when the run destination is set to Mac, but when I change the run destination to Apple Vision Pro, it doesn't compile and throws some errors.

let cxxString = std.string("This is a C++ string")
let swiftString = String(cxxString)

No exact matches in call to initializer

So, what I get from this is that the mixing Swift and C++ doesn't support visionOS for now.

Accepted Answer

Mixing swift & C++ works for visionOS. I am able to pass std:vector<int>, std:vector<float> as arguments to C++ methods successfully, for visionOS targets.

But I suspect there are some gaps/bugs, in visionOS support. Or, maybe some compiler settings/includes is creating some confusion.

Because I can see the problem you mention, in my builds as well.

The line let swiftString = String(cxxString) throws compiler error No exact matches in call to initializer.

The line let cxxString = std.string("This is a C++ string") causes runtime error, silently creating a zero length string.

The reported issue is still happening on Xcode 15.1 Beta 2 that was published yesterday.

VisionOS Support for Mixing Swift and C++ Interoperability
 
 
Q