6 ms·
If it's a simple enough binary interface, sure. Simplicity is really important. C++ has standardized ABIs but you just don't see people dynamically loading a C
by matheusmoreira 4y ago
If it's a simple enough binary interface, sure. Simplicity is really important.
C++ has standardized ABIs but you just don't see people dynamically loading a C++ shared object, obtaining a pointer to a C++ object and calling its member functions. They always do it through the C ABI if they do it at all. Some would rather rewrite stuff in C than put up with that. C++ ABIs are so obnoxiously complex the only things that dare to touch it are C++ compilers and even they have been known to break things. I just get this incredibly hopeless feeling whenever I try to contemplate how I'd handle a C++ exception from Python code. The only reasonable answer I can think of is I wouldn't, I'd just disable C++ exceptions instead and hope for the best.
People use C because it's just simple symbols and unchanging calling conventions. Look up symbols to get function pointers, put parameters in specific registers, call the function and off you go. Nobody will interface with Rust if the ABI is too complex. Given the language's many high level abstractions, I think it's likely we'll see a repeat of the C++ situation above.
- AshamedCaptain 4y ago> C++ has standardized ABIs but you just don't see people dynamically loading a C++ shared object, obtaining a pointer to a C++ object and calling its member functions. They always do it through the C ABI if they do it at all. That does not match my experience at all. From the top of my head, Qt (QtPlugin) does C++ ABI plugins, and they do that almost transparently in a myriad of platforms. > I just get this incredibly hopeless feeling whenever I try to contemplate how I'd handle a C++ exception from Python code. At one point I could even catch C++ exceptions from Java, and viceversa (see gcj).
- matheusmoreira 4y ago> From the top of my head, Qt (QtPlugin) does C++ ABI plugins This? https://wiki.qt.io/Plugins https://wiki.qt.io/Plugins > provides a bunch of macros that helps us create the C-function that generates the plugin object Looks like it uses the C ABI. > At one point I could even catch C++ exceptions from Java, and viceversa (see gcj). I'm interested in the details of that. Perhaps the GCJ compiler generated some sort of bridge between the languages?
- flohofwoe 4y agoAs far as I'm aware, the Qt DLLs themselves just expose C++ class APIs directly without going through a C API wrapper, that's why there's a separate Qt SDK for every MSVC version (and all hell breaks loose if you accidentally mismatch DLLs).
- matheusmoreira 4y agoYes, Qt itself is a C++ library. AshamedCaptain was talking about the plugin interface though which is just dynamically loadable modules for Qt applications. I looked it up and those things export C functions that return the C++ objects and meta-object system data.
- AshamedCaptain 4y agoThey export _C++_ functions. You can't return a C++ object from a C function (unless you start making assumptions from the C++ ABI).
- AshamedCaptain 4y ago> looks like it uses the C ABI. Only in the sense that C++ itself "uses the C ABI". This is still C++ ABI; most if not all of the plugin types inherit from C++ classes and there are no code wrappers generated whatsoever (except the usual moc slot unmarshallers if you want to be really picky, but these are not going to be ones you'll be calling). These macros are there for discovery and enumeration/reflection. After you have constructed the root plugin object, you are calling it through the C++ ABI, vtables included (since actually you call it through the base class). > I'm interested in the details of that. Perhaps the GCJ compiler generated some sort of bridge between the languages? The C++ exception/"personality" ABI is kind of designed for this, which is one of the reasons it may appear complex and/or slow. Just search for gcj exceptions for examples.
- matheusmoreira 4y ago> Only in the sense that C++ itself "uses the C ABI" Not at all. Qt literally uses the C ABI as a protocol for plugins. I have no doubt they chose it because it's the only inferface simple and stable enough for the task. It is relatively simple and stable precisely because there are zero C++ features involved. I thought that alone was damning enough so I didn't elaborate further. What you said is absolutely true: the objects returned by those C functions are accessed by the real C++ ABI with vtables and everything. That's a massive problem and they shouldn't have been designed this way. Look at the mess this causes: https://forum.qt.io/topic/119767/qt-plugin-fixing-c-abi-problems-with-external-c https://forum.qt.io/topic/119767/qt-plugin-fixing-c-abi-prob... > I know that C++ don't have Aplication Binary Interface - ABI compatibility between compilers (example: MSVC, MingW, Clang and ICC). > Besides that, the C++ ABI compatibility can be broken even using the same compiler with different version (example: MSVC 2003 and MSVC 2019). > Example: If the main C++ application was compiled using MSVC 2003, all plugin developers MUST HAVE and MUST USE the exactly same compiler to build your projects, in this case MSVC 2003. Which is not good. > Looking in the internet I found a way to fix that: Creating a C Wrapper of my C++ Interface Abstract Class. The solution is, of course, to get rid of all this C++ business and restrict yourself to the C ABI. This is apparently the fate of every ABI that isn't C. When even that is annoying enough to deal with, nobody is ever going to want to deal with anything more complex.
- flohofwoe 4y ago> but you just don't see people dynamically loading a C++ shared object ...that's exactly how Autodesk Maya plugin DLLs work, and it's a PITA, because DLLs need to be recompiled with a specific MSVC version for each new Maya version. Qt DLLs on Windows are also separate for each MSVC version.
- matheusmoreira 4y agoYeah... I suppose that's why we don't see people doing it anymore. That's what I hope at least.
- pjmlp 4y agoI suggest learning how WinUI works.
- leni536 4y agoThere are a ton of shared C++ libraries in Linux distros that maintain ABI stability without wrapping with a C ABI.
- matheusmoreira 4y agoABI stability between C++ libraries and applications built with the same compiler, sure. In other words, the only code that touches C++ code is other C++ code.
- leni536 4y agoNot necessarily same compiler. You can use clang and safely link to C++ libraries shipped by the disto, which are compiled by gcc typically.
- kibwen 4y agoThe Clang and GCC ABIs aren't quite 100% compatible there, e.g. you can't pass __int128 between libraries compiled by different compilers.
- leni536 4y agoOK, I did not know that. I assume it equally affects C and C++ though.
- matheusmoreira 4y agoSource for the __int128 incompatibility claim, posted on another thread: https://news.ycombinator.com/item?id=34147105 https://news.ycombinator.com/item?id=34147105 https://faultlore.com/blah/c-isnt-a-language/ https://faultlore.com/blah/c-isnt-a-language/ I'd like to emphasize that these tests were simple function calls between C code generated by C compilers. I can only imagine the hell that might be unleashed if similar tests for C++ ABIs are created.
- leni536 4y ago
- spacechild1 4y ago> C++ has standardized ABIs No, it has not, at least not as part of the C++ standard. > but you just don't see people dynamically loading a C++ shared object, obtaining a pointer to a C++ object and calling its member functions We certainly do, but we have to make sure the ABIs match each other - unless we use COM-style interfaces.
- matheusmoreira 4y ago> No, it has not, at least not as part of the C++ standard. The implementations seem to have converged on a de facto standard ABI. https://itanium-cxx-abi.github.io/cxx-abi/abi.html https://itanium-cxx-abi.github.io/cxx-abi/abi.html > we have to make sure the ABIs match each other Which essentially boils down to ensuring everybody is using the exact same compiler, sometimes the same compiler version even. > unless we use COM-style interfaces These aren't C++ ABIs though.
- spacechild1 4y ago> The implementations seem to have converged on a de facto standard ABI. Except for Windows. And probably a few other platforms.
- pjmlp 4y agoI see it all the time, Qt, MFC, VCL, FireMonkey, all the way back to Mac OS AppToolbox, PowerPlant, CSet++, OWL. And then there is COM, and with WinRT it is even better as it uses .NET CLS types for the metadata.
- matheusmoreira 4y agoYou can absolutely link to all of those things if you have a compiler toolchain to do it. Can you dynamically load Qt's C++ DLLs from an arbitrary scripting language and use them via foreign function interfaces though? I don't think so. I remember exploring even Boost Python source code and finding extern "C" deep in its bowels so I seriously doubt people are doing this.
- pjmlp 4y agoNaturally not, because CPython doesn't have a C++ FFI, which is a Python problem, not C++'s.
- matheusmoreira 4y ago> because CPython doesn't have a C++ FFI Does anything have a C++ FFI?
- pjmlp 4y agoC++ for starters (obviously),.NET via C++/CLI, D, Swift is in the process of getting one.
- matheusmoreira 4y ago> C++ for starters (obviously),.NET via C++/CLI Of course C++ compilers are capable of generating C++ ABI code. They are not foreign function interfaces, they are native. > D They have some rather interesting commentary on the matter. https://dlang.org/spec/cpp_interface.html https://dlang.org/spec/cpp_interface.html. > Being 100% compatible with C++ means more or less adding a fully functional C++ compiler front end to D. > making a D compiler with such capability unimplementable > the solutions have been: > Support the COM interface (but that only works for Windows). > Laboriously construct a C wrapper around the C++ code. > Use an automated tool such as SWIG to construct a C wrapper. > Reimplement the C++ code in the other language. > Give up. The pragmatic approach mentioned apparently consists of matching the D and C++ ABIs as much as possible just to make the problem tractable. Even after that impressive effort, the foreign interface is basic and incomplete. Special member functions like constructors and destructors are not supported, for instance. Notably, there is no exceptions support! > Swift is in the process of getting one Swift had to embed a literal C++ compiler inside itself in order to even make such a proposition viable. https://faultlore.com/blah/c-isnt-a-language/ https://faultlore.com/blah/c-isnt-a-language/ > Not even Swift has the stomach for this stuff.
- kibwen 4y ago> Given the language's many high level abstractions, I think it's likely we'll see a repeat of the C++ situation above. The goals in the OP seem to suggest that the objective is not to have a bespoke ABI that encapsulates the entire Rust language, but rather to build a relatively minor shim on top of the existing C ABI that supports some useful Rust patterns (e.g. Option, Result) which should be possible to express straightforwardly in many languages. If this effort succeeds then I can absolutely see it supplanting the C ABI for at least the dynamically-loaded-Rust-from-Rust use case.
- matheusmoreira 4y agoYeah, seems reasonable.
- jcelerier 4y ago> C++ has standardized ABIs but you just don't see people dynamically loading a C++ shared object, obtaining a pointer to a C++ object and calling its member functions ... Sorry what? This is exactly how every c++ plugin system I've worked with works. There are even some industry standards that work like this, e.g. VST3: https://steinbergmedia.github.io/vst3_dev_portal/pages/Technical+Documentation/VST+Module+Architecture/Index.html https://steinbergmedia.github.io/vst3_dev_portal/pages/Techn... ; there are dozens of companies that provide hosts for such plugins and thousands that provide the actual plugins, all built with different toolchains, compilers etc, so obviously it works
- gavinray 4y agoBut VST3 uses a COM-like protocol which is quite a bit different than an unstructured C++ ABI though You obtain an IUnknown and query for the supported interfaces