by shmerl · 10y ago · 0 comments
https://en.wikipedia.org/wiki/Name_mangling#How_different_co...
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.
Calling into C++ is generally a big problem in any programming language though, and for most, you have to write a C wrapper.
You can perfectly write a C++ library and expose a C-safe subset. I've done so many times.
The problem arises if you need to expose objects (with virtual methods), which C obviously doesn't have as the ffi is basically restricted to plain functions and record types.
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...
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?)
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: