Passing CFLAGS and LDFLAGS to cgo

Posted on Sun 12 July 2015 in misc

I was trying to build which wraps the C libshout library with Go.  On Linux this worked fine, but on OpenBSD this was a bit more of a challenge as cgo could not find libshout.  (install libshout with pkg_add first)

Although -lshout is specified as an LDFLAG  in the go source code, I needed to to specify the include path for libshout and also link to libspeex (-lsppex)

CFLAGS and LDFLAGS can be passed to go build as environmental variables so I set:

export CGO_CFLAGS='-I/usr/local/include'

export CGO_LDFLAGS='-L/usr/local/lib -lshout -lspeex'

Then I is was able to do

go build -x

(the -x is very useful for checking was gets passed to cgo)