5 ms·
You end up having to toss a layer of c around your c++ last time I checked. Does c++ even have an abi yet?
by alexnewman 11y ago
You end up having to toss a layer of c around your c++ last time I checked. Does c++ even have an abi yet?
- roblabla 11y agowell, not a standard one. That's what extern "C" is for : if you want a standardized API, C always had one.
- deleted 11y ago[deleted]
- gpderetta 11y agoNeither C nor C++ have a standardized ABI, if by standardized you mean part of their respective ISO standard. Nor they could, as the many of ABI properties (names, calling conventions, layout, object and executable file format) are intimately platform (as in OS and CPU) specific. Most platform officially specify their C ABI, and many also specify their C++ ABI (usually in therm of the C one). Itanium C++ ABI (plus the platform specific psABIs) has become the de facto C++ ABI for many unix and unix-like platforms.
- Manishearth 11y agoYou can call into Rust directly from C++ (with a header file). But yeah, the reverse requires a C layer. rust-bindgen has experimental C++ support which we use for spidermonkey but it isn't perfect.
- sitkack 11y ago> You can call into Rust directly from C++ (with a header file). Can you tell me more about this?
- steveklabnik 11y agoThis is C and I tossed it together VERY quickly but $ cat hello.rs #[no_mangle] pub extern fn plus_one(x: isize) -> isize { x + 1 } $ cat hello.c #include<stdio.h> extern int plus_one(int); int main() { int result = plus_one(5); printf("result is %d\n", result); return 0; } $ rustc hello.rs --crate-type=dylib $ gcc hello.c -lhello -L . $ LD_LIBRARY_PATH=. ./a.out result is 6
- sitkack 11y agoI get the calling into Rust from C via no_mangle + extern. I was looking for the header only C++ to Rust path.
- steveklabnik 11y agoHm, I might have misunderstood either you or the OP, then. A C++ header would include the same extern declaration, though it would mark it with the C ABI as well.
- Manishearth 11y agoIt's the same way you call into C from C++. When Rust has a no_mangle extern C function to the universe it's just a C function. Your C++ header just needs to mark the fn as extern C and you're done, you can call it as you would call any other C/++ function.
- sitkack 11y agoOh, I thought there was a magical way to call from C++ into Rust using only a C++ header w/o no_mangle/extern.
- pjmlp 11y agoC never had one standard ABI, not one that I can read in ANSI C document. What happens is that C ABI == OS ABI, when the OS is written in C. This is not the case in the mainframe OSes that are still alive, for example. So people have come to expect C ABI as being some kind of standard. On an OS written in pure C++, the OS vendor C++ compiler would be the ABI. Having said this, there are efforts to partially standardize the C++ ABI: https://isocpp.org/blog/2014/05/n4028 https://isocpp.org/blog/2014/05/n4028 Also many vendors use the Intel's C++ Itanium ABI as reference.
- gpderetta 11y agoNote that the standardization effort is not what most people expect. It really is just a call for: a) each platform to document its C++ABI. b) each platform to offer a ABI stable variant of the standard library as an option. Both points are really already the norm and the default on many platforms. For example GCC on most OSs follows the documented Itanium ABI while libstdc++ has been ABI stable for a long time at least on Linux. The notable exception is windows and MSVC. While the C ABI is documented, I believe that C++ ABI is pretty much "whatever MSVC does" and had to be reverse enginereed. Additionally MSVC reservers the right to break its library ABI at every major release (and in fact it does).
- pjmlp 11y agoSure, hence why I said partially. However being mainly a .NET/JVM developer nowadays, I just follow C++ standardization on the sidelines. Thanks for correction.