SWIG for Go C++ Bindings - Some Confusion

875 views
Skip to first unread message

netpoetica

unread,
Sep 23, 2013, 11:46:08 PM9/23/13
to golan...@googlegroups.com
Hey folks,

I'm trying to use swig to wrap some C++ functionality in go for an experiment, but I'm not really understanding the process from these docs: http://www.swig.org/Doc2.0/Go.html#Go_running_swig

I also have searched swig on this board, but I think some of the people here working with swig are a few steps ahead of me because I haven't quite gotten to where there problems began. From the swig docs:

% swig -go example.i
% gcc -c -fpic example.c
% gcc -c -fpic example_wrap.c
% gcc -shared example.o example_wrap.o -o example.so
% 6g example.go
% 6c example_gc.c
% gopack grc example.a example.6 example_gc.6
% 6g main.go  # your code, not generated by SWIG
% 6l main.6

First thing, right of the bat, what is that .i file and why do I need it? From what I have read on the web, it's something generated by visual studio to describe the preprocessing that occured before VS compiles your program. However, I do not/cannot/wont use Visual Studio, and I can't seem to get a good handle on what a .i file even is/does.

Ideally I should just be able to run

swig -go myFile.h

Where myFile includes a bunch of other header and C++ files and swig just compiles all of it for me :) obviously, this is ideally, and I am ready to have to work harder for this.

If I try to run that command I get from Mac OSX Mountain Lion terminal:

SWIG -go: -intgosize option required but not specified

Which I imagine is something that would have been defined in the .i file if I had one.

Can anyone advise or point me in a good direction where I could better understand this process?

Thanks a ton,
- Keith

Ian Lance Taylor

unread,
Sep 23, 2013, 11:53:14 PM9/23/13
to netpoetica, golang-nuts
On Mon, Sep 23, 2013 at 8:46 PM, netpoetica <kthros...@gmail.com> wrote:
>
> I'm trying to use swig to wrap some C++ functionality in go for an
> experiment, but I'm not really understanding the process from these docs:
> http://www.swig.org/Doc2.0/Go.html#Go_running_swig

These days you should create a .swig or .swigcxx file and use the go
tool. The go tool will run SWIG for you.


> % swig -go example.i

> First thing, right of the bat, what is that .i file and why do I need it?
> From what I have read on the web, it's something generated by visual studio
> to describe the preprocessing that occured before VS compiles your program.
> However, I do not/cannot/wont use Visual Studio, and I can't seem to get a
> good handle on what a .i file even is/does.

No, that is not the .i file. The .i file is the SWIG input file, as
described at http://swig.org/Doc2.0/SWIG.html#SWIG_nn3 . It's the
same file that the go tool will look for as a .swig or .swigcxx file.


> Ideally I should just be able to run
>
>
> swig -go myFile.h
>
>
> Where myFile includes a bunch of other header and C++ files and swig just
> compiles all of it for me :) obviously, this is ideally, and I am ready to
> have to work harder for this.

That almost works but you generally need more than that, which is what
the .swig file is for.


> If I try to run that command I get from Mac OSX Mountain Lion terminal:
>
>
> SWIG -go: -intgosize option required but not specified
>
>
> Which I imagine is something that would have been defined in the .i file if
> I had one.

No, that does need to be provided on the command line. The go tool
will provide it for you. When running SWIG by hand, it's
-intgosize=64 when building for amd64, -intgosize=32 when building for
386 or arm.

Ian

netpoetica

unread,
Sep 23, 2013, 11:56:00 PM9/23/13
to golan...@googlegroups.com
Firstly, apologizing for mispelling of there when meaning "their." Some research on swig showed me the .i file is an interface file: http://www.swig.org/Doc1.3/SWIG.html#SWIG_nn46 and http://www.swig.org/Doc1.3/Introduction.html#Introduction_nn5 but if you look at google results for "what is a .i file" (https://www.google.com/search?q=what+is+a+.i+file&oq=what+i&aqs=chrome.0.69i59j69i57j69i60j0l2.1475j0&sourceid=chrome&ie=UTF-8) you can see where my confusion about Visual Studio came from.

Nonetheless, I am still getting that intgosize error on Mac OSX but traced it to this thread: https://groups.google.com/forum/#!searchin/golang-nuts/intgosize/golang-nuts/U-1Ih--TIKc/5vth9hTL9SkJ

And I looks like I just need to upgrade swig (I downloaded tar that I thought was most recent but it was version 2.0.11)

netpoetica

unread,
Sep 24, 2013, 12:03:31 AM9/24/13
to golan...@googlegroups.com, netpoetica
Thanks a bunch Ian - is there anywhere I can find documentation about .swig files and how to write one? Can't find a single thing on google.

Additionally, I do not see anything in the go tools that looks like it will run swig from what I can tell, or any option to be able to provide. In the docs you linked there were three mentions of swig and swigfiles but they were in the list and remove section - not build.

Ian Lance Taylor

unread,
Sep 24, 2013, 11:54:18 AM9/24/13
to netpoetica, golang-nuts
On Mon, Sep 23, 2013 at 9:03 PM, netpoetica <kthros...@gmail.com> wrote:
> Thanks a bunch Ian - is there anywhere I can find documentation about .swig
> files and how to write one? Can't find a single thing on google.

Same link I posted before: http://swig.org/Doc2.0/SWIG.html#SWIG_nn3 .


> Additionally, I do not see anything in the go tools that looks like it will
> run swig from what I can tell, or any option to be able to provide. In the
> docs you linked there were three mentions of swig and swigfiles but they
> were in the list and remove section - not build.

Documentation for the SWIG support was only added recently, but the
support does exist. In the current development version of the docs
(which will soon be the 1.2 release):
http://tip.golang.org/cmd/go/#hdr-Calling_between_Go_and_C .

Ian

netpoetica

unread,
Sep 24, 2013, 10:21:14 PM9/24/13
to golan...@googlegroups.com, netpoetica
Thank again Ian, this is helpful, but I am still running into some strangeness with what looks like the path/to/cppheadersandfiles - when I put the code:

/*
#include "libs/main.h"
*/
import "C"

It looks like go knows to look for libs/main.h, but then all of the includes in main.h confuse it and go cannot find them. I am imaging there must be a command line flag to tell cgo where to look for all C++ files or that maybe I am supposed to create a swig file that details the location of every included include, but this is the level of documentation that I am not finding

Ian Lance Taylor

unread,
Sep 24, 2013, 11:12:55 PM9/24/13
to netpoetica, golang-nuts
On Tue, Sep 24, 2013 at 7:21 PM, netpoetica <kthros...@gmail.com> wrote:
> Thank again Ian, this is helpful, but I am still running into some
> strangeness with what looks like the path/to/cppheadersandfiles - when I put
> the code:
>
> /*
> #include "libs/main.h"
> */
> import "C"
>
> It looks like go knows to look for libs/main.h, but then all of the includes
> in main.h confuse it and go cannot find them. I am imaging there must be a
> command line flag to tell cgo where to look for all C++ files or that maybe
> I am supposed to create a swig file that details the location of every
> included include, but this is the level of documentation that I am not
> finding

The .swig file can not be used to tell the compiler where to find
header files. That is not what it is for.

You didn't provide enough information to know what is going wrong, but
it is reasonably likely that you can fix this using a #cgo CFLAGS
-I<directory> option. See http://golang.org/cmd/cgo for how #cgo
works and see your compiler documentation for the -I option.

Ian
Reply all
Reply to author
Forward
0 new messages