cgo or SWIG for thick bindings

706 views
Skip to first unread message

Gour

unread,
Jun 19, 2014, 1:30:27 PM6/19/14
to golan...@googlegroups.com
Hello,

starting with Go which I plan to use for both my web needs as well as
for writing multi-platform desktop application.

One of the requirements I have for desktop application is to call 3rd
party C library - sized of the *.h providing API (function calls and
#define-s) is ~28K which means not as big as GTK, but also not just
calling few functions.

I'd like to provide more Go-ish API - thicker bindings - instead of just
using thin wrapper, so wonder which procedure is recommended: Cgo or
SWIG?


Any input is appreciated...


Sincerely,
Gour

--
The senses are so strong and impetuous, O Arjuna,
that they forcibly carry away the mind even of a man
of discrimination who is endeavoring to control them.

Ian Lance Taylor

unread,
Jun 19, 2014, 1:37:01 PM6/19/14
to Gour, golang-nuts
On Thu, Jun 19, 2014 at 10:29 AM, Gour <go...@atmarama.net> wrote:
>
> starting with Go which I plan to use for both my web needs as well as
> for writing multi-platform desktop application.
>
> One of the requirements I have for desktop application is to call 3rd
> party C library - sized of the *.h providing API (function calls and
> #define-s) is ~28K which means not as big as GTK, but also not just
> calling few functions.
>
> I'd like to provide more Go-ish API - thicker bindings - instead of just
> using thin wrapper, so wonder which procedure is recommended: Cgo or
> SWIG?

For a pure C library, use cgo.

SWIG is more complex and I wouldn't bother with it unless you need C++ support.

Ian

Justin Israel

unread,
Jun 19, 2014, 3:53:58 PM6/19/14
to Ian Lance Taylor, Gour, golang-nuts
On this same topic, Ian do you know if previously mentioned SWIG issues related to linking to 3rd party C++ libraries would now be working in Go 1.3? I had seen more that one thread on this in the past. I had initially tried to use a SWIG approach to linking to OpenColorIO, but my tests were resulting in it not linking to libOpenColorIO properly. So I had dropped that idea and completed my bindings with a CGO approach. And then I did the same for my OpenImageIO binding.  



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ian Lance Taylor

unread,
Jun 19, 2014, 5:49:14 PM6/19/14
to Justin Israel, Gour, golang-nuts
On Thu, Jun 19, 2014 at 12:53 PM, Justin Israel <justin...@gmail.com> wrote:
> On this same topic, Ian do you know if previously mentioned SWIG issues
> related to linking to 3rd party C++ libraries would now be working in Go
> 1.3? I had seen more that one thread on this in the past. I had initially
> tried to use a SWIG approach to linking to OpenColorIO, but my tests were
> resulting in it not linking to libOpenColorIO properly. So I had dropped
> that idea and completed my bindings with a CGO approach. And then I did the
> same for my OpenImageIO binding.

That's a little vague as I don't know just which issues you mean.
However, I can say that in Go 1.3 the SWIG support is different, as
briefly mentioned at http://golang.org/doc/go1.3#swig . It no longer
requires using a shared library.

Ian

Stephen Gutekanst

unread,
Jun 19, 2014, 7:40:42 PM6/19/14
to golan...@googlegroups.com, ia...@golang.org, go...@atmarama.net
Justin,

Not certain but it sounds like you mean issue 6696. If so, I've submitted a CL for it but it didn't make Go 1.3, I'm going to try and get it into Go 1.4 if possible.

Stephen

Discussion: https://groups.google.com/forum/#!topic/golang-dev/B_gY96GKVc8

Gour

unread,
Jun 20, 2014, 2:31:22 AM6/20/14
to golan...@googlegroups.com
Ian Lance Taylor <ia...@golang.org> writes:

> For a pure C library, use cgo.

OK.

> SWIG is more complex and I wouldn't bother with it unless you need C++
> support.

No need for C++ support, just ability to tweak Go's side of the bindings
a bit.


Sincerely,
Gour

--
Not by merely abstaining from work can one achieve freedom
from reaction, nor by renunciation alone can one attain perfection.

Justin Israel

unread,
Jun 20, 2014, 3:45:46 PM6/20/14
to Gour, golang-nuts
@Ian/Stephen - Ya that looks to be similar to what I had experienced.


I haven't tried SWIG for this solution since then, as I just assumed it was problematic for wrapping external C++ shared libs and stuck with the CGO solution. But obviously that requires hand-rolling the C-shim layer. 



Reply all
Reply to author
Forward
0 new messages