draffensperger / go-interlang

Examples of calls between Go and C/C++ (and how to call a Go shared object from Node/Ruby/Python/Java)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

easier to wrap c or c++?

opened this issue · comments

I am trying to write a wrapper for this:
https://github.com/TommyKaneko/Sketchup-API-C-Wrapper

They have a c and cpp wrapper over the c.
SO which is easier from a golang perspective ?
Would really appreciate your advice as I have never really done much of this golang to c work before.

Thanks for asking. While C++ is a lot more expressive, it's harder to call from other languages because of "name mangling" (see https://en.wikipedia.org/wiki/Name_mangling#C++), which has to do with how C++ names functions in binaries/shared libaries.

So if you are OK with either way, I would recommend making a C language wrapper. It's OK to make C functions that then call out to C++ functions, just use "extern C" to make sure the C functions aren't name-managed in the export.

On the other hand, if you prefer C++, you can use SWIG to automate the generation of those wrapper functions for you. See this example here: https://github.com/draffensperger/go-interlang/tree/master/go_to_cxx/swig

@draffensperger thankyou for the advice. really helps. SWIG code gen looks like a good candidate to me. Will try it