Search code examples
c++iosobjective-cstatic-librariesobjective-c++

Objective C++ stable ABI?


Question 1: I read that Objective-C under the hood uses the C ABI? Is that correct?

Question 2: But in that case how does Objective-C so easily interop itself with C++?

I can build a static library with Objective-C and C++ while the public headers are purely Objective-C. Question 3: Is this stable enough to be used by partners in their iOS apps irrespective of their compiler versions ?


Solution

  • Question 1: I read that Objective-C under the hood uses the C ABI? Is that correct?

    Sort of. Objective-C has its own linkage rules for class registration at runtime etc. Function calling (message passing) conventions do match those of C, though. In any case, it's considered stable.

    Question 2: But in that case how does Objective-C so easily interop itself with C++?

    C and C++ also interoperate quite straightforwardly, so I guess the question is why not?

    The main thing the compiler needs to ensure is that C++-typed members (ivars) of Objective-C classes correctly follow C++ constructor/destructor rules, and that ARC pointers to Objective-C typed objects inside C++ classes correctly perform ARC retain/release operations on C++ construction/destruction. Those aspects do indeed work correctly.

    Question 3: Is this stable enough to be used by partners in their iOS apps irrespective of their compiler versions?

    Your use of the C++ standard library (libc++) must be compatible with the version used by the consumer of your library. Apple has so far ensured that this works well across OS and toolchain versions, however. If you use very modern standard library features (e.g. std::variant), you may find that this restricts the possible deployment targets.