5 ms·
How does it manage C++ bindings? Rust struggled with that, and it has a lot of issues because of compiler specific name decorations and etc. https://en.wikiped
by shmerl 11y ago
How does it manage C++ bindings? Rust struggled with that, and it has a lot of issues because of compiler specific name decorations and etc.
https://en.wikipedia.org/wiki/Name_mangling#How_different_compilers_mangle_the_same_functions https://en.wikipedia.org/wiki/Name_mangling#How_different_co...
- pierrec 11y agoThe difficulty in importing C++ libraries from other languages don't really represent a Rust issue, but rather a C++ issue, of course. As to the very interesting question of how it's handled here, it looks like part of the project (the "qtdrv" folder) is a shared library written in C++ that includes Qt, wraps the desired functionality, and exports it as C-style symbols (with extern "C"). This is a pretty standard way of circumventing the name mangling problem. What I find interesting is that everything appears to be wrapped into a single exported function "qtdrv". Might be a wrapping technique that I'm not aware of, or maybe I'm completely misreading the source. I'm actually quite interested in knowing how the wrapping code was generated, having projects where the same kind of C++ wrapping is required.
- shmerl 11y agoI guess success of such approach depends on how well C++ logic can be wrapped in C. It's not always straightforward or even possible.
- Elv13 11y agoI haven't looked into the source, but Qt use a "MOC" pre-compilation system to generate introspection data. Once you have access to that, you don't have to mess with C++ symbols anymore. Same for the objects properties, you can get/set them by name.
- majewsky 11y agoIn principle, yes, but a) access via MOC is certainly slower than compiling a direct function call into a single `call` opcode, and b) MOC only covers signals, slots, and Q_INVOKABLE methods. For QObjects, that means that the remaining methods are not accessible. And methods for non-QObject-derived classes are also inaccessible.
- rhodysurf 11y agoLook in the doc folder of the project. It explains the design and memory layout. Its not as difficult as rust.
- shmerl 11y agoI'm not sure how it can be easier or harder for some languages (to make bindings to C++). C++ is just hard to make bindings to. Even if you do it from C to C++.
- archimedespi 11y agoRust has #[no_mangle] and ffi types. I've done some FFI in Rust, and I've found it to be pretty pleasant. What are the "lot of issues" you're talking about?
- iherbig 11y agoRust has good C FFI. The Book (https://doc.rust-lang.org/book/ffi.html https://doc.rust-lang.org/book/ffi.html) itself says that Rust is not able to call directly into a C++ library.
- geofft 11y agoRust can call directly into a C++ library as long as it uses the C ABI: there aren't bindings to (a compiler-specific implementation of) the C++ type system. Go is just the same. This isn't really a language restriction, more a result of how rich the C type system is and how compiler-specific it is. The only languages that have direct C++ interop are those that directly use a C++ compiler (like Objective-C++). It appears that GoQt has this giant pile of wrappers around C++ types, casting pointers back and forth: https://raw.githubusercontent.com/visualfc/goqt/master/qtdrv/cdrv.cpp https://raw.githubusercontent.com/visualfc/goqt/master/qtdrv... I would have expected this to require an `extern "C"` declaration. I'm sort of surprised it doesn't, but maybe the common C++ ABIs match the C ones if only C types are passed to and from the function? (Also, is this file auto-generated?)
- shmerl 11y agoI wonder though why C++ couldn't come up with a standard for names decoration? It would solve tons of such issues.
- emmelaich 11y agoThe answer is in the wikipedia article you linked to earlier... [it] would not suffice to guarantee C++ compiler interoperability and it might even create a false impression that interoperability is possible and safe when it isn't.
- deleted 11y ago[deleted]
- amelius 11y agoI'm wondering how they deal with Go's garbage collector, which is allowed to move objects in memory.
- danieldk 11y agoThe Go garbage collector only concerns itself with memory allocated in Go, not with memory allocated in C-land. So, it doesn't move C objects. (C objects are typically freed using finalizers, or an explicit method call.) Of course, moving can cause problems for Go pointers in C land, but keeping Go pointers beyond a cgo function call will be prohibited in Go 1.6: https://tip.golang.org/doc/go1.6#cgo https://tip.golang.org/doc/go1.6#cgo
- amelius 11y agoYes, the callback functions (registered in Qt, calling back into Go) will probably be a problem then. I guess they will need to handle the situation through a kind of lookup-table approach, which of course will come at a cost.