[HELP] Cgo linking with static/dynamic libraries

4,068 views
Skip to first unread message

Tibor

unread,
Mar 17, 2012, 6:26:02 AM3/17/12
to golan...@googlegroups.com
Hello,

I searched through the mailing-list and understood certain things about cgo but I want to be sure if I am correct.

Cgo cannot link with static libraries (*.a files).
Cgo can only link with dynamic libraries (*.so files).
go install is based on convention not configuration, so no custom Makefiles can be hooked up to the install process.

I would like the user of my cgo package to do:
without her needing to install the C libraries before.

How could I achieve this?

When I copied the *.so files into my package, the go test worked, however I am not sure whether it is the proper way to go.
Will it work for everyone if I include dynamic libraries created on my computer?

Thanks,

Tibor

minux

unread,
Mar 17, 2012, 6:39:24 AM3/17/12
to Tibor, golan...@googlegroups.com
On Sat, Mar 17, 2012 at 6:26 PM, Tibor <teab...@gmail.com> wrote:
Hello,

I searched through the mailing-list and understood certain things about cgo but I want to be sure if I am correct.

Cgo cannot link with static libraries (*.a files).
Currently, it can't.
With a upcoming CL, it will get this capability, provided the static library is compiled -fPIC (e.g. CFLAGS=`go env GOGCCFLAGS`)
To link a static library, you can name the static libary file by this (-lxxx won't work).
#cgo LDFLAGS: libstatic.a
Cgo can only link with dynamic libraries (*.so files).
go install is based on convention not configuration, so no custom Makefiles can be hooked up to the install process.

I would like the user of my cgo package to do:
without her needing to install the C libraries before.

How could I achieve this?
If your C code is simple, you can just bundle the C source with the Go source, and they will be statically linked
together look for example, weekly.golang.org/misc/cgo/life (it can be built simply using 'go build misc/cgo/life/main.go',
and the Go tool is smart enough to compile the C code by gcc)

Tibor

unread,
Mar 17, 2012, 6:46:11 AM3/17/12
to golan...@googlegroups.com, Tibor
Thank you very much for that quick answer!

Cgo cannot link with static libraries (*.a files).
Currently, it can't.
With a upcoming CL, it will get this capability, provided the static library is compiled -fPIC (e.g. CFLAGS=`go env GOGCCFLAGS`)
To link a static library, you can name the static libary file by this (-lxxx won't work).
#cgo LDFLAGS: libstatic.a
 
isn't it : #cgo LDFLAGS: -lmylib ?

If your C code is simple, you can just bundle the C source with the Go source, and they will be statically linked
together look for example, weekly.golang.org/misc/cgo/life (it can be built simply using 'go build misc/cgo/life/main.go',
and the Go tool is smart enough to compile the C code by gcc)

The problem is that the library was created with autoconf/automake so I would prefer using libraries directly.

How about copying the shared libraries into my package? Is it okay?

minux

unread,
Mar 17, 2012, 6:55:09 AM3/17/12
to Tibor, golan...@googlegroups.com
On Sat, Mar 17, 2012 at 6:46 PM, Tibor <teab...@gmail.com> wrote:
Thank you very much for that quick answer!

Cgo cannot link with static libraries (*.a files).
Currently, it can't.
With a upcoming CL, it will get this capability, provided the static library is compiled -fPIC (e.g. CFLAGS=`go env GOGCCFLAGS`)
To link a static library, you can name the static libary file by this (-lxxx won't work).
#cgo LDFLAGS: libstatic.a
 
isn't it : #cgo LDFLAGS: -lmylib ?
It won't work for CL 5822049, it filtered all -lxxx style options from LDFLAGS.
Of course, before the CL is merged, you have to use -lmylib and stick to dynamic linking.

If your C code is simple, you can just bundle the C source with the Go source, and they will be statically linked
together look for example, weekly.golang.org/misc/cgo/life (it can be built simply using 'go build misc/cgo/life/main.go',
and the Go tool is smart enough to compile the C code by gcc)

The problem is that the library was created with autoconf/automake so I would prefer using libraries directly.

How about copying the shared libraries into my package? Is it okay?
You mean the binary .so file? I'd rather not if you want a source release. For binary releases, this won't matter,
but be sure to use appropriate (DY)LD_LIBRARY_PATH or your binary might not start correctly.

unread,
Mar 17, 2012, 7:47:47 AM3/17/12
to golan...@googlegroups.com, Tibor
On Saturday, March 17, 2012 11:39:24 AM UTC+1, minux wrote:
With a upcoming CL, it will get this capability, provided the static library is compiled -fPIC (e.g. CFLAGS=`go env GOGCCFLAGS`)

Why is -fPIC necessary?

minux

unread,
Mar 17, 2012, 7:55:59 AM3/17/12
to ⚛, golan...@googlegroups.com, Tibor
The reason is simple, our linker don't have full support for static relocations.
(Although I'd like to fix this after Go 1 so that we can build static linking binary even if using cgo)
Reply all
Reply to author
Forward
0 new messages