Re: Cgo directive with os and arch

1,211 views
Skip to first unread message

Steven Robertson

unread,
Jul 31, 2012, 5:19:07 PM7/31/12
to golan...@googlegroups.com
I mean:

/*
#cgo windows/amd64 CFLAGS: -IC:/Lua_x86_64/include
#cgo windows/amd64 LDFLAGS: -LC:/Lua_x86_64
*/

Either 'windows' or 'amd64' will result in the directive having an effect. Together they don't work, and it would be useful.

On Tuesday, 31 July 2012 15:05:21 UTC+1, Steven Robertson wrote:
Hello

I have the following #cgo directives for windows, darwin and linux. I'd like to have different library and include paths for 32-bit and 64-bit libraries, and it seems it should work on x86_64 as written below. It works if I use $GOOS or $GOARCH on it's own but not with $GOOS/$GOARCH as below (I'm using go version 1.0.2)

/*
#cgo linux LDFLAGS: -llua
#cgo darwin LDFLAGS: -llua
#cgo windows LDFLAGS: -llua51
#cgo windows/386 CFLAGS: -IC:/Lua/include
#cgo windows/386 LDFLAGS: -LC:/Lua/lib
#cgo windows/amd64 CFLAGS: -IC:/C:/Lua_x86_64/include
#cgo windows/amd64 LDFLAGS: -LC:/C:/Lua_x86_64
*/

I couldn't find many examples and I'm new to this so probably I'm doing it wrong. Any suggestions?

Cheers

Steve

minux

unread,
Jul 31, 2012, 8:58:23 PM7/31/12
to Steven Robertson, golan...@googlegroups.com
On Tue, Jul 31, 2012 at 5:19 PM, Steven Robertson <stevero...@gmail.com> wrote:
I mean:

/*
#cgo windows/amd64 CFLAGS: -IC:/Lua_x86_64/include
#cgo windows/amd64 LDFLAGS: -LC:/Lua_x86_64
*/

Either 'windows' or 'amd64' will result in the directive having an effect. Together they don't work, and it would be useful.
according to http://golang.org/cmd/cgo/, i think it means the corresponding flag
is only for the $GOOS/$GOARCH combination.

Steven Robertson

unread,
Aug 1, 2012, 3:29:12 AM8/1/12
to golan...@googlegroups.com, Steven Robertson
I'm not sure I know what you mean. I thought that's what I'm doing.

I found this link which maybe the change to provide this feature on the cgo directive:

It's helpful at least because it's the only other example I could find:

// #cgo linux/amd64 CFLAGS: -DLINUX_ON_AMD64=1

I wasn't even sure what values for $GOOS and $GOARCH are allowed, but I figure that 'linux', 'darwin' and 'windows' are all allowed and use 'amd64' for 64-bit x86 and '386' to match against 32-bit x86. It would be good to have more examples, but so far as I can see what I've done is correct so maybe it's a bug. I'm so new to this that I am not at all sure.

Regards

Steve

Sebastien Binet

unread,
Aug 1, 2012, 5:41:38 AM8/1/12
to Steven Robertson, golan...@googlegroups.com
Steve,

On Wed, Aug 1, 2012 at 9:29 AM, Steven Robertson
<stevero...@gmail.com> wrote:
> I'm not sure I know what you mean. I thought that's what I'm doing.
>
> I found this link which maybe the change to provide this feature on the cgo
> directive:
> http://codereview.appspot.com/4121048/
>
> It's helpful at least because it's the only other example I could find:
>
> // #cgo linux/amd64 CFLAGS: -DLINUX_ON_AMD64=1
>
> I wasn't even sure what values for $GOOS and $GOARCH are allowed, but I
> figure that 'linux', 'darwin' and 'windows' are all allowed and use 'amd64'
> for 64-bit x86 and '386' to match against 32-bit x86. It would be good to
> have more examples, but so far as I can see what I've done is correct so
> maybe it's a bug. I'm so new to this that I am not at all sure.

I can't comment on whether this is a bug but I am wondering if a
suitable alternative wouldn't be to create, say,
cgoflags_<goos>_<goarch>.go files with the wanted cgo incantations.

-s

Steven Robertson

unread,
Aug 1, 2012, 5:46:28 AM8/1/12
to golan...@googlegroups.com, Steven Robertson
Hi Sebastien

Is it possible to include files dependent on os and arch?

Certainly I hope to avoid writing any makefiles :)

Cheers

Steve

Sebastien Binet

unread,
Aug 1, 2012, 5:55:40 AM8/1/12
to Steven Robertson, golan...@googlegroups.com
On Wed, Aug 1, 2012 at 11:46 AM, Steven Robertson
<stevero...@gmail.com> wrote:
> Hi Sebastien
>
> Is it possible to include files dependent on os and arch?

yep, even w/o Makefiles.
see:
http://golang.org/pkg/go/build/

so, either add build constraints to your file:
// +build linux,386 darwin

or name your file accordingly:
cgoflags_linux.go # built only on linux
cgoflags_linux_amd64.go # only on linux/amd64

I guess you should choose one way and stick to it...

hth,
-s

Steven Robertson

unread,
Aug 1, 2012, 6:10:27 AM8/1/12
to golan...@googlegroups.com, Steven Robertson
Ah, thanks for the suggestion. That looks interesting and I'm sure I'll find that useful, however reading the linked page it occurred to me to try ',' as the separator between os/arch instead of '/' and that appears to be the correct syntax for the #cgo directive!

So it seems the documentation is wrong and that it should be $GOOS,$GOARCH and not $GOOS/$GOARCH.

The following example seems to work for me:

/*
#include <stdlib.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#cgo windows,386 CFLAGS: -IC:/Lua/include
#cgo windows,386 LDFLAGS: -LC:/Lua/lib
#cgo windows,amd64 CFLAGS: -IC:/Lua_x86_64/include
#cgo windows,amd64 LDFLAGS: -LC:/Lua_x86_64
#cgo windows LDFLAGS: -llua51
#cgo darwin LDFLAGS: -llua
#cgo linux LDFLAGS: -llua
*/

Also, it does seem to be necessary to include the #cgo directives in the same comment-block immediately prior to the import "C" line.

Thanks & Regards

Steve
Reply all
Reply to author
Forward
0 new messages