Where is the Objective-C Plus Plus (ObjC++) documentation?

I'm working to wrap a C++ libary so it can be called from ObjC/Swift. I managed to find some useful tidbits in the doc for the GCC flag `-fobjc-call-cxx-cdtors`. And then I also was able to track down some older stuff in the Internet Archive: http://web.archive.org/web/20101203170217/http://developer.apple.com/library/mac/#/web/20101204020949/http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html


But other than that, there doesn't seem to be a lot of doc out there. Hopefully that's a good sign and "things just work" as I'd expect... but I'd like to refer to "current documentation" to help make sure I'm not forgetting any potential gotchas, etc. Is there a current version of the guide for ObjC/C++ interop anywhere? I was hoping for something similar to the excellent guide for working with C/ObjC from Swift.

Any help is appreciated... thanks!

Answered by DTS Engineer in 227068022

I don’t do C++ so I don’t have a lot of insight to share here. However, I thought I’d address this point:

That older doc I found in the internet archive is helpful... but it pre-dates the releases of ARC, C++ 11, and C++ 14.

The standard go-to document for ARC, Objective-C Automatic Reference Counting, does cover C++ issues. I’m not sure if it’s sufficient for your needs, but it’s worth looking at.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hello jeff.olson,

I am unaware of any documentation that describes how to do this. In fact, it shouldn't "just work" at all. C++ is not compatible with Swift. I can tell you what I've done, but I don't think that is documented anywhere.


I create a header file "Foo.h" and put my Objective-C interface there. Next, I have a header "FooInternal.h" and add an anonymous category that has a handle to the underlying C++ object. Finally, the "Foo.mm" file uses that C++ object and wraps all operations in Objective-C. Swift will only see "Foo.h" and import it all as Objective-C. It is pretty labour-intensive, but I don't know of any other way to do it.

Another way to do it is to wrap the library in a thin extern-C function layer, and call that directly from Swift. Passing C pointers (e.g. callbacks) is better in Swift than it used to be, but if there's a lot of that involved, a Obj-C++ wrapper is probably a better choice.

Sorry... to be clear: I know you can't call C++ from Swift. What I am trying to do is wrap the C++ calls with ObjC (which in turn can be called from Swift). But it is the ObjC/C++ interop documentation that I can't locate (and hope "just works"). Sorry for the confusion...

But that is what I'm trying to say. There is no interoperability. Apple probably deleted that one chapter you referenced just to discourage people from doing it. You most definitely do not want to ever use "-fobjc-call-cxx-cdtors". If you do it correctly, then you shouldn't ever need it. If you do need, it, then you've done it wrong.


Here are a couple of remaining documents. They are fairly low-level and won't help you much:

https://developer.apple.com/library/content/technotes/tn2185/_index.html#//apple_ref/doc/uid/DTS10004200

https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/CPPRuntimeEnv.html#//apple_ref/doc/uid/TP40001666


Can you describe how you were intending to wrap this library? They key is that Objective-C is just C and nothing else. There are some low-level, highly-optimized C functions that do the messaging magic and make it into an OO language. The brackets are just syntactic sugar. But because it is C, you can make use of the fact that pointer types don't matter. You can create pointers to C++ object and they can exist in C and Objective-C. Your Objective-C++ source code can recognize that those pointers are to C++ objects and use them. They must be C++ pointers though. That is the only way to bridge the languages.


And if you think about it, pretty much every structure in any high-level language is going to be a pointer. So that is why I can say there is no interoperability while describing how to do it. If you are clever enough with the pointers, you can have Swift work with literally any language. You will always have to have a C layer to bridge the pointers. Objective-C++ just happens to include that C layer natively.


What is your target platform anyway? If you are on a Mac, you can also wrap your C++ code in a stand-alone executable or XPC and call it that way. Depending on what you are doing, this may be just as fast and/or something you need to do anyway. Don't forget, your C++ library might assume you have C++ exceptions too. You can't let those escape into Swift.

I think you're answering a different question than Jeff is asking. When it comes to Objective C++ and it's interaction with "pure" C++ it actually just works - no obvious gotchas.

Right. I'd like to see a "current" version of this documentation if it exists: http://web.archive.org/web/20101203170217/http://developer.apple.com/library/mac/#/web/20101204020949/http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html


That older doc I found in the internet archive is helpful... but it pre-dates the releases of ARC, C++ 11, and C++ 14. And it seems likely that there might have been some new things to be aware of since then. Basically, I'm trying to make sure that I "know what I don't know"


Having said that, it sounds like the answer to my question is that this documentation doesn't exist anymore (which is unfortuante imho). Luckily, given the "just works" comment above and a lack of issues with ObjC/C++ interop questions coming up in online searches... I'm feeling like I'm probably in pretty good shape.


I'm going to hold this open for a couple more days to see if lightening strikes and someone shares something. But thanks to everyone for taking the time to comment... I appreciate it.

Accepted Answer

I don’t do C++ so I don’t have a lot of insight to share here. However, I thought I’d address this point:

That older doc I found in the internet archive is helpful... but it pre-dates the releases of ARC, C++ 11, and C++ 14.

The standard go-to document for ARC, Objective-C Automatic Reference Counting, does cover C++ issues. I’m not sure if it’s sufficient for your needs, but it’s worth looking at.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks!

The OP's original question specifically asked about calling all of this from Swift. He seemed interested in actually using an obscure flag like -fobjc-call-cxx-cdtors and has now found out about -fobjc-arc-exceptions. It sounds like this project is going to crash and burn.


It might be useful if the OP specifically said what C++ library this is and what C++ this is. Is it the old-school OOP or is it a modern meta-template version? Does it use exceptions? If so, each wrapper method is going to need about 50-100 lines of exception-handling boilerplate. Is there multi-threading involved? Let's see him throw some exception-throwing, thread-spawning C++ templates into a Swift closure. 👿

Where is the Objective-C Plus Plus (ObjC++) documentation?
 
 
Q