Mischiefblog
I make apps for other people

A Go (cgo) gotcha

Posted by Chris Jones
On June 24th, 2014 at 16:24

Permalink | Trackback | Links In |

Comments Off on A Go (cgo) gotcha
Posted in General

If you get a lot of errors like:

37: error: use of undeclared identifier 'create_message'

in your Go interface to a C library, double check your imports. You can’t use imports in the style:

/*
* #cgo CFLAGS: -I../libdir
* #cgo LDFLAGS: -L../libdir -lmylib
* #include "mylib.h"
*/
import (
"C"
"unsafe"
)

Instead, use each import on a separate line, or make the first import “C” so that cgo will pick up the header include:

/*
* #cgo CFLAGS: -I../libdir
* #cgo LDFLAGS: -L../libdir -lmylib
* #include "mylib.h"
*/
import "C"
import "unsafe"

Also, make sure that if you’re using a local shared library (.so) that you symlink it to a .so name:

$ gcc -c -fPIC mylib.c -o mylib.o
$ gcc -shared -W1,-soname,libmylib.so.1 -o libmylib.so.1.0.0 mylib.o
$ ln -s libmylib.so.1.0.0 libmylib.so

Comments are closed.