Ordering of linker flags using GCCGO

742 views
Skip to first unread message

Timon ter Braak

unread,
Dec 10, 2012, 7:11:08 AM12/10/12
to golan...@googlegroups.com
Hi,

I am using the GLPK library from within Go.
Manually compiling & linking everything works fine.
However, trying to leverage the go build tool results in undefined
references. This is caused by the order of the command line parameters.

The Go build tool does this:

$ GOPATH=`pwd` go build -compiler gccgo -gccgoflags='-lglpk -lm'
-ldflags='-lglpk -lm' glpk

$ gccgo -o $WORK/glpk/_obj/a.out -lglpk -lm $WORK/glpk/_obj/main.o
-Wl,-( -Wl,-)

A manual compile&link like this gives the same results:

$ gccgo -o main -lglpk -lm libglpk.go main.go

Manually changing the order of the given flags, does work fine:

gccgo -o main libglpk.go main.go -lglpk -lm

Can somebody explain this behavior, and more important:
'Can this be changed in the go build tool, or should we aim for another
solution?'

Thanks in advance,
Timon

Ian Lance Taylor

unread,
Dec 11, 2012, 8:39:56 PM12/11/12
to Timon ter Braak, golan...@googlegroups.com
gccgo uses the system linker, and the system linker on an ELF system
requires that -l options follow the input files. This is something we
should control in the go tool.

Ian

minux

unread,
Dec 15, 2012, 2:34:48 PM12/15/12
to Timon ter Braak, golan...@googlegroups.com
On Mon, Dec 10, 2012 at 8:11 PM, Timon ter Braak <timont...@gmail.com> wrote:
Hi,

I am using the GLPK library from within Go.
Manually compiling & linking everything works fine.
However, trying to leverage the go build tool results in undefined references. This is caused by the order of the command line parameters.

The Go build tool does this:

$ GOPATH=`pwd` go build -compiler gccgo -gccgoflags='-lglpk -lm' -ldflags='-lglpk -lm' glpk
you should put "-lglpk -lm' into argument for -gccgoflags.

Quote from go help build (emphasis added by me):
-gccgoflags 'arg list'
arguments to pass on each gccgo compiler/*linker* invocation
-ldflags 'flag list'
arguments to pass on each 5l, 6l, or 8l linker invocation
that is, -ldflags is for gc's linker, maybe we should make it clearer, but i don't know who.
do you have any suggestions?

Timon ter Braak

unread,
Dec 17, 2012, 11:20:53 AM12/17/12
to golan...@googlegroups.com
On 12/15/2012 08:34 PM, minux wrote:> $ GOPATH=`pwd` go build
-compiler gccgo -gccgoflags='-lglpk -lm'
> -ldflags='-lglpk -lm' glpk
>
> you should put "-lglpk -lm' into argument for -gccgoflags.
>
> Quote from go help build (emphasis added by me):
> -gccgoflags 'arg list'
> arguments to pass on each gccgo compiler/*linker* invocation
> -ldflags 'flag list'
> arguments to pass on each 5l, 6l, or 8l linker invocation
> that is, -ldflags is for gc's linker, maybe we should make it clearer,
> but i don't know who.
> do you have any suggestions?

I put them in both to be sure, but as you can see in my example, I
already put them into the gccgoflags. The flags are 'used' when invoking
gccgo, put are in the wrong place.
See Ian's explanation on this...

Ian Lance Taylor

unread,
Dec 17, 2012, 3:33:57 PM12/17/12
to Timon ter Braak, golan...@googlegroups.com
Conventionally two flags are used for the GCC linker: LDFLAGS and
LIBS. We could use a single flag with the go tool, but if so it has
to come after all the libraries. That is, change
return b.run(".", p.ImportPath, "gccgo", "-o", out, buildGccgoflags,
ofiles, "-Wl,-(", ldflags, "-Wl,-)")
to
return b.run(".", p.ImportPath, "gccgo", "-o", out, ofiles, "-Wl,-(",
ldflags, "-Wl,-)", buildGccgoflags)
in gccgcToolchain.ld in src/cmd/go/build.go.

Do you want to try that?

Ian

Timon ter Braak

unread,
Dec 17, 2012, 4:23:19 PM12/17/12
to Ian Lance Taylor, Timon ter Braak, golan...@googlegroups.com
On 12/17/2012 09:33 PM, Ian Lance Taylor wrote:
> Conventionally two flags are used for the GCC linker: LDFLAGS and
> LIBS. We could use a single flag with the go tool, but if so it has
> to come after all the libraries. That is, change
> return b.run(".", p.ImportPath, "gccgo", "-o", out, buildGccgoflags,
> ofiles, "-Wl,-(", ldflags, "-Wl,-)")
> to
> return b.run(".", p.ImportPath, "gccgo", "-o", out, ofiles, "-Wl,-(",
> ldflags, "-Wl,-)", buildGccgoflags)
> in gccgcToolchain.ld in src/cmd/go/build.go.
>
> Do you want to try that?
>
> Ian
My application links against 3 libraries, and moving the specified flags
after the object files works fine.
So, I can confirm that this 'fix' solves my linking problem.

Does this affect any other compilation work, or should we expect this to
become standard in the go tools?

Thanks!
Timon

minux

unread,
Dec 18, 2012, 12:51:13 PM12/18/12
to Timon ter Braak, golan...@googlegroups.com
On Tue, Dec 18, 2012 at 5:23 AM, Timon ter Braak <ti...@terbraak.org> wrote:
On 12/17/2012 09:33 PM, Ian Lance Taylor wrote:
Conventionally two flags are used for the GCC linker: LDFLAGS and
LIBS.  We could use a single flag with the go tool, but if so it has
to come after all the libraries.  That is, change
        return b.run(".", p.ImportPath, "gccgo", "-o", out, buildGccgoflags,
ofiles, "-Wl,-(", ldflags, "-Wl,-)")
to
        return b.run(".", p.ImportPath, "gccgo", "-o", out, ofiles, "-Wl,-(",
ldflags, "-Wl,-)", buildGccgoflags)
in gccgcToolchain.ld in src/cmd/go/build.go.

Do you want to try that?

Ian
My application links against 3 libraries, and moving the specified flags after the object files works fine.
So, I can confirm that this 'fix' solves my linking problem.
Thanks for confirmation.
This problem is fixed at tip, the Go tool in Go 1.1 will also warn the user if he passed the wrong flags
("-compiler gccgo -ldflags xxx", for example, will be warned).

lup...@gmail.com

unread,
Apr 15, 2014, 6:26:17 PM4/15/14
to golan...@googlegroups.com, timont...@gmail.com
On Monday, December 10, 2012 12:11:08 PM UTC, Timon ter Braak wrote:
Hi,

I am using the GLPK library from within Go.
 
Hi,

May I aks whether you are willing to share your GLPK binding with the public?
I just started to bind GLPK myself, but may be you have a working binding which you want to share with others?

Regards,
Łukasz
Reply all
Reply to author
Forward
0 new messages