Go 1.2 / SWIG / C++

507 views
Skip to first unread message

Justin Israel

unread,
Nov 29, 2013, 5:43:04 PM11/29/13
to golan...@googlegroups.com
Ultimately my goal is to call into some functionality from OpenColorIO / OpenImageIO, so I'm first trying to get a handle on how to use SWIG and C++.
I managed to build and play around with the callback example, but I'm just failing to grasp how to build these test calls. 

Here is what I have:

I get errors about it not finding standard includes. so I assume I am just completely setting things up wrong:

$ go run main.go
# swigtest/oiio
In file included from oiio/oiio.go:5:
In file included from ./oiio.h:4:
/usr/local/include/OpenImageIO/imageio.h:52:10: fatal error: 'vector' file not found
#include <vector>
^
1 error generated.
# swigtest/ocio
In file included from ocio/ocio.go:6:
In file included from ./ocio.h:4:
/usr/local/Cellar/opencolorio/1.0.8/include/OpenColorIO/OpenColorIO.h:33:10: fatal error: 'exception' file not found
#include <exception>
^
1 error generated.

Any help would be greatly appreciated! What I have now is just pieced together from the examples I was able to find. Sorry if this problem sounds stupidly simple. I just have no experience with SWIG yet, and in combination with Go, I don't know what I don't know :-)

Ian Lance Taylor

unread,
Nov 29, 2013, 11:34:01 PM11/29/13
to Justin Israel, golang-nuts
On Fri, Nov 29, 2013 at 2:43 PM, Justin Israel <justin...@gmail.com> wrote:
>
> Here is what I have:
> https://gist.github.com/justinfx/7712860
>
> I get errors about it not finding standard includes. so I assume I am just
> completely setting things up wrong:
>
> $ go run main.go

What's the output of "go run -x main.go"?

Ian

Justin Israel

unread,
Nov 29, 2013, 11:44:39 PM11/29/13
to Ian Lance Taylor, golang-nuts
$ go run -x main.go 
WORK=/var/folders/5f/413nmn3s4hb9bm6dbrny6gsh0000gn/T/go-build270643742
mkdir -p $WORK/swigtest/ocio/_obj/
mkdir -p $WORK/swigtest/
cd /Users/justin/src/go/src/swigtest/ocio
pkg-config --cflags OpenColorIO
mkdir -p $WORK/swigtest/oiio/_obj/
cd /Users/justin/src/go/src/swigtest/oiio
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/swigtest/oiio/_obj/ -- -I $WORK/swigtest/oiio/_obj/ oiio.go
cd /Users/justin/src/go/src/swigtest/ocio
pkg-config --libs OpenColorIO
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/swigtest/ocio/_obj/ -- -I/usr/local/Cellar/opencolorio/1.0.8/include -I $WORK/swigtest/ocio/_obj/ ocio.go
# swigtest/oiio
In file included from oiio/oiio.go:5:
In file included from ./oiio.h:4:
/usr/local/include/OpenImageIO/imageio.h:52:10: fatal error: 'vector' file not found
#include <vector>
         ^
1 error generated.
# swigtest/ocio
In file included from ocio/ocio.go:6:
In file included from ./ocio.h:4:
/usr/local/Cellar/opencolorio/1.0.8/include/OpenColorIO/OpenColorIO.h:33:10: fatal error: 'exception' file not found
#include <exception>
         ^
1 error generated.

Ian Lance Taylor

unread,
Nov 30, 2013, 11:17:38 AM11/30/13
to Justin Israel, golang-nuts
On Fri, Nov 29, 2013 at 8:44 PM, Justin Israel <justin...@gmail.com> wrote:
> $ go run -x main.go
> WORK=/var/folders/5f/413nmn3s4hb9bm6dbrny6gsh0000gn/T/go-build270643742
> mkdir -p $WORK/swigtest/ocio/_obj/
> mkdir -p $WORK/swigtest/
> cd /Users/justin/src/go/src/swigtest/ocio
> pkg-config --cflags OpenColorIO
> mkdir -p $WORK/swigtest/oiio/_obj/
> cd /Users/justin/src/go/src/swigtest/oiio
> /usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/swigtest/oiio/_obj/ --
> -I $WORK/swigtest/oiio/_obj/ oiio.go
> cd /Users/justin/src/go/src/swigtest/ocio
> pkg-config --libs OpenColorIO
> /usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/swigtest/ocio/_obj/ --
> -I/usr/local/Cellar/opencolorio/1.0.8/include -I $WORK/swigtest/ocio/_obj/
> ocio.go
> # swigtest/oiio
> In file included from oiio/oiio.go:5:
> In file included from ./oiio.h:4:
> /usr/local/include/OpenImageIO/imageio.h:52:10: fatal error: 'vector' file
> not found
> #include <vector>

Oh--you're mixing SWIG and cgo. That doesn't work at present. If you
are using SWIG, you can't use import "C". If you are using import
"C", you can't use SWIG.

See misc/swig in the sources for examples of how to use SWIG.

Ian

Justin Israel

unread,
Nov 30, 2013, 6:05:23 PM11/30/13
to golan...@googlegroups.com, Justin Israel
Ah ok, I've gotten that much working now, thanks! Had some crossed wires between the examples I had been looking up.
Obviously now I have a few more complications.

1) I think I am just supposed to reference the wrapped functions as if they were local to the package?
// ocio.go
func PrintVersion() {
    //x := ocio.GetVersion()
    x := GetVersion()
    fmt.Println(x)
}

2) I'm seeing a problem where its not linking properly to the .so that swig is generating
$ go run -x main.go 
...
swig -go -intgosize 64 -module ocio -soname swigtest-ocio-ocio-swigcxx.so -o $WORK/swigtest/ocio/_obj/ocio_wrap.cxx -outdir $WORK/swigtest/ocio/_obj/ -c++ ocio.swigcxx
...
/usr/local/go/pkg/tool/darwin_amd64/6l -o $WORK/command-line-arguments/_obj/exe/main -L $WORK -L /Users/justin/src/go/pkg/darwin_amd64 -r /Users/justin/src/go/pkg/darwin_amd64/swig:$WORK/swigtest/ocio/_obj/ 
...
dyld: Library not loaded: swigtest-ocio-ocio-swigcxx.so
  Referenced from: /var/folders/5f/413nmn3s4hb9bm6dbrny6gsh0000gn/T/go-build882053055/command-line-arguments/_obj/exe/main
  Reason: image not found
signal: trace/BPT trap

3) I didn't even really expect it to work yet, because with cgo I was able to specify the pkg-config opts, but I am not clear where I can get SWIG to pick up the includes/libs for OpenColorIO?

Ian Lance Taylor

unread,
Nov 30, 2013, 7:50:01 PM11/30/13
to Justin Israel, golang-nuts
On Sat, Nov 30, 2013 at 3:05 PM, Justin Israel <justin...@gmail.com> wrote:
>
> 1) I think I am just supposed to reference the wrapped functions as if they
> were local to the package?
> // ocio.go
> func PrintVersion() {
> //x := ocio.GetVersion()
> x := GetVersion()
> fmt.Println(x)
> }

Yes.


> 2) I'm seeing a problem where its not linking properly to the .so that swig
> is generating
> $ go run -x main.go
> ...
> swig -go -intgosize 64 -module ocio -soname swigtest-ocio-ocio-swigcxx.so -o
> $WORK/swigtest/ocio/_obj/ocio_wrap.cxx -outdir $WORK/swigtest/ocio/_obj/
> -c++ ocio.swigcxx
> ...
> /usr/local/go/pkg/tool/darwin_amd64/6l -o
> $WORK/command-line-arguments/_obj/exe/main -L $WORK -L
> /Users/justin/src/go/pkg/darwin_amd64 -r
> /Users/justin/src/go/pkg/darwin_amd64/swig:$WORK/swigtest/ocio/_obj/
> ...
> dyld: Library not loaded: swigtest-ocio-ocio-swigcxx.so
> Referenced from:
> /var/folders/5f/413nmn3s4hb9bm6dbrny6gsh0000gn/T/go-build882053055/command-line-arguments/_obj/exe/main
> Reason: image not found
> signal: trace/BPT trap

I don't know what is required to make this work on Darwin. Somebody
familiar with Darwin will need to investigate.


> 3) I didn't even really expect it to work yet, because with cgo I was able
> to specify the pkg-config opts, but I am not clear where I can get SWIG to
> pick up the includes/libs for OpenColorIO?

You're right, that is a weak spot. You can do it by setting the CC
environment variable to "gcc -I DIR -L DIR" but it's not convenient.

Ian

Justin Israel

unread,
Nov 30, 2013, 9:51:52 PM11/30/13
to Ian Lance Taylor, golang-nuts

> /var/folders/5f/413nmn3s4hb9bm6dbrny6gsh0000gn/T/go-build882053055/command-line-arguments/_obj/exe/main
>   Reason: image not found
> signal: trace/BPT trap

I don't know what is required to make this work on Darwin.  Somebody
familiar with Darwin will need to investigate.


> 3) I didn't even really expect it to work yet, because with cgo I was able
> to specify the pkg-config opts, but I am not clear where I can get SWIG to
> pick up the includes/libs for OpenColorIO?

You're right, that is a weak spot.  You can do it by setting the CC
environment variable to "gcc -I DIR -L DIR" but it's not convenient.

Ian

Ya that got me further:
$ CC="gcc -I/usr/local/Cellar/opencolorio/1.0.8/include -L/usr/local/Cellar/opencolorio/1.0.8/lib -lOpenColorIO"  go build -x -v -work main.go

Hopefully someone with OSX experience can offer some guidance. I had to leave the -work flag on there because it was not linking in the swig .so and deleting the work directory, and I wanted to see if it would even work if I manually used it. 

$ DYLD_FALLBACK_LIBRARY_PATH=/var/folders/5f/413nmn3s4hb9bm6dbrny6gsh0000gn/T/go-build595629156/swigtest/ocio/_obj/ ./main

dyld: lazy symbol binding failed: Symbol not found: _GetVersion
  Referenced from: /var/folders/5f/413nmn3s4hb9bm6dbrny6gsh0000gn/T/go-build595629156/swigtest/ocio/_obj//swigtest-ocio-ocio-swigcxx.so
  Expected in: flat namespace

dyld: Symbol not found: _GetVersion
  Referenced from: /var/folders/5f/413nmn3s4hb9bm6dbrny6gsh0000gn/T/go-build595629156/swigtest/ocio/_obj//swigtest-ocio-ocio-swigcxx.so
  Expected in: flat namespace

SIGTRAP: trace trap
PC=0x7fff63ee606d
signal arrived during cgo execution

runtime.cgocall(0x1b1e70, 0x7a1ed8)
/usr/local/go/src/pkg/runtime/cgocall.c:149 +0x11b fp=0x7a1ec0
swigtest/ocio.GetVersion(0xc21000a140, 0x0)
swigtest/ocio/_obj/ocio_gc.c:33 +0x34 fp=0x7a1ed8
swigtest/ocio.PrintVersion()
/Users/justin/src/go/src/swigtest/ocio/ocio.go:6 +0x1e fp=0x7a1f40
main.main()
/Users/justin/src/go/src/swigtest/main.go:9 +0x1a fp=0x7a1f48
runtime.main()
/usr/local/go/src/pkg/runtime/proc.c:220 +0x11f fp=0x7a1fa0
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1394 fp=0x7a1fa8
[..snip..]

So yea, I think I will just wait for a nice blog post to come along some day, explaining how to do a SWIG / Go approach that is wrapping another 3rd party C++ lib. This whole thing is beyond me now :-) 

Thanks for the help Ian!

Reply all
Reply to author
Forward
0 new messages