using the external linker against a static library

249 views
Skip to first unread message

James Bardin

unread,
Jun 28, 2013, 5:32:12 PM6/28/13
to golan...@googlegroups.com
I'm banging my head against a wall here. I built a a go binary against libGraphicsMagick.a a few times, and now I can't replicate the build. I must have not had a clean environment, but I cannot figure out what I'm missing now.

Using `GraphicsMagick-config` I can generate the following gcc command to compile a test program:

gcc -o convert convert.c -fopenmp -g -O2 -Wall -pthread -L/usr/local/lib -L/usr/lib -lGraphicsMagick -llcms -ltiff -lfreetype -ljasper -ljpeg -lpng16 -lwmflite -lbz2 -lz -lm -lgomp -lpthread -I/usr/local/include/GraphicsMagick

This works with `gcc -static` too.


I *thought* I passed these same linker options into `go build`, but no matter what I do, I cannot get the external linker to find all the libs, and I get tons of "undefined reference to `cos'" and so on.

What I "documented" doing was:

GO_LD_FLAGS=-linkmode external -extldflags "-Wl,-Bstatic -lGraphicsMagick -llcms -ltiff -lfreetype -ljasper -ljpeg -lpng16 -lwmflite -lbz2 -lz -lm -lgomp -lpthread -Wl,-Bdynamic"
go build -ldflags "$GO_LD_FLAGS"


But now it doesn't work (I also had a fully static binary built and verified, but I added the -Wl,-Bstatic/dynamic options to dynamically link libc.)

Any idea what I could be missing?




Rodrigo Kochenburger

unread,
Jun 28, 2013, 6:29:13 PM6/28/13
to golan...@googlegroups.com
It looks like you're problem might be just some quoting issue. Try this:

GO_LD_FLAGS='-linkmode external -extldflags "-Wl,-Bstatic -lGraphicsMagick -llcms -ltiff -lfreetype -ljasper -ljpeg -lpng16 -lwmflite -lbz2 -lz -lm -lgomp -lpthread -Wl,-Bdynamic"'
go build -ldflags "$GO_LD_FLAGS"

Cheers

Carlos Castillo

unread,
Jun 28, 2013, 8:15:39 PM6/28/13
to golan...@googlegroups.com
According to http://golang.org/cmd/cgo/:

use CGO_LDFLAGS


On Friday, June 28, 2013 2:32:12 PM UTC-7, James Bardin wrote:

Carlos Castillo

unread,
Jun 28, 2013, 8:16:50 PM6/28/13
to golan...@googlegroups.com
Also if you run go build/install/run/etc... with -x you can see the command lines run to see how GCC is executed.

James Bardin

unread,
Jun 28, 2013, 9:48:15 PM6/28/13
to golan...@googlegroups.com


On Friday, June 28, 2013 8:16:50 PM UTC-4, Carlos Castillo wrote:
Also if you run go build/install/run/etc... with -x you can see the command lines run to see how GCC is executed.

On Friday, June 28, 2013 5:15:39 PM UTC-7, Carlos Castillo wrote:
According to http://golang.org/cmd/cgo/:

use CGO_LDFLAGS


I don't know why I didn't try that! CGO_CFLAGS and CGO_LDFLAGS fixed the issue of cramming it all in on the commandline.
export CGO_CFLAGS='-I/usr/local/include/GraphicsMagick'
export CGO_LDFLAGS='-Wl,-Bstatic -lGraphicsMagick -llcms -ltiff -lfreetype -ljasper -ljpeg -lpng16 -lwmflite -lbz2 -lz -lm -lgomp -Wl,-Bdynamic'

Thanks!

Carlos Castillo

unread,
Jun 28, 2013, 11:28:04 PM6/28/13
to golan...@googlegroups.com
BTW, if you're the one making the package, you can put libs and cflags in the cgo comments:

// # cgo LDFLAGS: -Wl,-Bstatic -lGraphicsMagick -llcms -ltiff -lfreetype -ljasper -ljpeg -lpng16 -lwmflite -lbz2 -lz -lm -lgomp -Wl,-Bdynamic
// # cgo CFLAGS: -I/usr/local/include/GraphicsMagick
// #include <stuff.h>
// .....
import "C"

CGO_*FLAGS should only be needed for specifying things specific to your machine such as custom include/lib dirs or compile options. If it's a flag needed to build the package (like the libs you use) then it should be in the source.
Reply all
Reply to author
Forward
0 new messages