CGO CFlags for different platforms

1,966 views
Skip to first unread message

OmarShariffDontLikeIt

unread,
Jul 13, 2012, 5:04:27 AM7/13/12
to golang-nuts
Hi!

I'm trying to bring my old CGO package (Tidy for Go: github.com/
JalfResi/GoTidy) upto date with GO1 and I've reached a small problem.
I need to inform CGO where tidy.h is located, and on OSX I do this
using the following:

#cgo CFLAGS: -I/usr/include

But under Ubuntu the tidy.h file is installed in a subdirectory /usr/
include/tidy. After a bit of googling I thought the following might
fix the problem:

#cgo CFLAGS: -I/usr/include
#cgo CFLAGS: -I-/usr/include/tidy

but with no success. How can I specify multiple locations for CGO to
look for include files?

In a related note, I may have to do the same for the LDFLAGS. Any
suggestions?

Cheers!
Ben

minux

unread,
Jul 13, 2012, 5:12:37 AM7/13/12
to OmarShariffDontLikeIt, golang-nuts
On Fri, Jul 13, 2012 at 5:04 AM, OmarShariffDontLikeIt <omarsharif...@gmail.com> wrote:
I'm trying to bring my old CGO package (Tidy for Go: github.com/
JalfResi/GoTidy
) upto date with GO1 and I've reached a small problem.
I need to inform CGO where tidy.h is located, and on OSX I do this
using the following:

#cgo CFLAGS: -I/usr/include

But under Ubuntu the tidy.h file is installed in a subdirectory /usr/
include/tidy. After a bit of googling I thought the following might
fix the problem:

#cgo CFLAGS: -I/usr/include
as a side note, i don't think you need to explicitly specify /usr/include. 
#cgo CFLAGS: -I-/usr/include/tidy
also, could you use pkg-config(1) to automatically get correct cflags and
ldflags?
#cgo pkg-config: tidy

but, it seems libtidy doesn't provide .pc file for pkg-config(1).

but with no success. How can I specify multiple locations for CGO to
look for include files?
#cgo linux CFLAGS: -I.... 
#cgo darwin CFLAGS: -I...

you can even use $GOOS/$GOARCH combination here:
#cgo linux/amd64 CFLAGS: ...

In a related note, I may have to do the same for the LDFLAGS. Any
suggestions?
The same hold for LDFLAGS.

OmarShariffDontLikeIt

unread,
Jul 13, 2012, 5:42:43 AM7/13/12
to golang-nuts
> > but with no success. How can I specify multiple locations for CGO to
> > look for include files?
>
> #cgo linux CFLAGS: -I....
> #cgo darwin CFLAGS: -I...
>
> you can even use $GOOS/$GOARCH combination here:
> #cgo linux/amd64 CFLAGS: ...

Ooo! Didn't know about that, very handy!

However, still no joy. When I type the following into bash on ubuntu I
get:

$ go get github.com/JalfResi/GoTidy
# github.com/JalfResi/GoTidy
main.go:4:18: fatal error: tidy.h: No such file or directory
compilation terminated.

But doing ls /usr/include/tidy shows that tidy.h is present.

Any ideas?

Cheers,
Ben

OmarShariffDontLikeIt

unread,
Jul 13, 2012, 7:27:19 AM7/13/12
to golang-nuts
Okay, looks like the problem was out of date files, the following
fixed everything:

go get -a -u github.com/etc.etc

On Jul 13, 10:42 am, OmarShariffDontLikeIt

Jeff Hodges

unread,
Jul 13, 2012, 12:34:49 PM7/13/12
to OmarShariffDontLikeIt, golang-nuts
Instead of teaching your code where those files might be on every
operating system, you can also use the CGO_*FLAGS environment
variables at build time. This might be handy to call out
in your README if your users have installed tidy somewhere unexpected.

CGO_CFLAGS=-I/my/inc/path CGO_LDFLAGS=-L/my/lib/path go get some/cgo/project

These two work with `go build`, as well.

Jiří Zárevúcky

unread,
Jul 18, 2012, 7:28:28 AM7/18/12
to golan...@googlegroups.com
On Friday, 13 July 2012 11:04:27 UTC+2, OmarShariffDontLikeIt wrote:

#cgo CFLAGS: -I/usr/include
#cgo CFLAGS: -I-/usr/include/tidy

but with no success. How can I specify multiple locations for CGO to
look for include files?


Can't you just specify all the directories?

 #cgo CFLAGS: -I/usr/include -I/usr/include/tidy

CC should just ignore non-existing directories. At least GCC does.

OmarShariffDontLikeIt

unread,
Jul 20, 2012, 7:15:05 AM7/20/12
to golang-nuts
To be honest I thought I had tried that. I'll give it a nother spin
later tonight, so thanks for the recommendation.
Reply all
Reply to author
Forward
0 new messages