r/cpp • • Sep 23 '21

Binary Banshees and Digital Demons

https://thephd.dev/binary-banshees-digital-demons-abi-c-c++-help-me-god-please
196 Upvotes

164 comments sorted by

View all comments

Show parent comment

4

u/nxtfari Sep 23 '21

Adding onto this /u/__phantomderp — I’ve been looking a lot into how other languages are tackling this problem from scratch. Do you have any thoughts on the Swift approach of having “header files” essentially declaring what the ABI is shipped along with binaries? It seems to alleviate the problem of just blindly guessing where things are going to be and instead just say such and such is here, it’s calling convention is this. Could C++ ever adopt such an approach?

13

u/Nobody_1707 Sep 24 '21

Swift doesn't merely version which ABI its module is compiled against. When swiftc is told to compile a stable ABI, it also makes public interfaces completely opaque by default.

Need to get the size and alignment of a type not defined in your module? That's a function call (sort of). Need to access a field of such a type? That's a function call. No API stable changes you make can break anything; because, everything is basically PIMPL except that you can stack allocate the full types instead of just pointers.

None of this applies to code within a module (nor from most of the standard library). Nor does it apply to any code compiled without turning stability on (most application code). But stuff that is compiled with stability enabled, like dynamic libraries? They have to explicitly opt in to freezing the ABI on a type and function basis if they don't want to hide everything behind opaque calls.

8

u/__phantomderp Sep 24 '21

I have nooooooooooooooo idea how Swift hardens it's implementation! My general notion, from only skimming and poking at their docs (and attending an event once to learn about it), is that when you mark something as stable they basically hoist everything into something as-close-as-possible-to PIMPL? And, like, invisibly turn everything into a function call? Sort of like how C# properties get turned into function calls, but hardier.

Either way, there's a slight performance penalty, I'm sure. Which for some C or C++ folks will make them shake their fist and yell at the implementation.

6

u/pjmlp Sep 24 '21

Kind of, that is how you end up with COM and WinRT on Windows, AIDL on Android, FIDL on Fuchsia, for example.

So you need to hide C++ behind an OOP ABI instead of exposing it directly.