Skip to content

cmd/go: build -x output does not describe CGO_LDFLAGS env variable #7249

@gopherbot

Description

@gopherbot

by andrey.gursky@e-mail.ua:

The simple program using external C libraries test_glib_minimal.go:

package main

// #cgo CFLAGS: -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
// #cgo LDFLAGS: -lglib-2.0
// #include <glib/gprintf.h>
// void foo(void) {
//      g_printf("foo\n");
// }
import "C"

func main(){
    C.foo()
}

go build test_glib_minimal.go
works.

go build -x test_glib_minimal.go
outputs commands:

WORK=/tmp/go-build922261626
mkdir -p $WORK/command-line-arguments/_obj/
cd /media/portable1/all1/projects/go-cgo
/usr/lib/go/pkg/tool/linux_amd64/cgo -objdir $WORK/command-line-arguments/_obj/ -- -I
$WORK/command-line-arguments/_obj/ -I/usr/include/glib-2.0
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include test_glib_minimal.go
/usr/lib/go/pkg/tool/linux_amd64/6c -F -V -w -I $WORK/command-line-arguments/_obj/ -I
/usr/lib/go/pkg/linux_amd64 -o $WORK/command-line-arguments/_obj/_cgo_defun.6 -D
GOOS_linux -D GOARCH_amd64 $WORK/command-line-arguments/_obj/_cgo_defun.c
gcc -I . -g -O2 -fPIC -m64 -pthread -print-libgcc-file-name
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/command-line-arguments/_obj/
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -o
$WORK/command-line-arguments/_obj/_cgo_main.o -c
$WORK/command-line-arguments/_obj/_cgo_main.c
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/command-line-arguments/_obj/
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -o
$WORK/command-line-arguments/_obj/_cgo_export.o -c
$WORK/command-line-arguments/_obj/_cgo_export.c
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/command-line-arguments/_obj/
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -o
$WORK/command-line-arguments/_obj/test_glib_minimal.cgo2.o -c
$WORK/command-line-arguments/_obj/test_glib_minimal.cgo2.c
gcc -I . -g -O2 -fPIC -m64 -pthread -o $WORK/command-line-arguments/_obj/_cgo_.o
$WORK/command-line-arguments/_obj/_cgo_main.o
$WORK/command-line-arguments/_obj/_cgo_export.o
$WORK/command-line-arguments/_obj/test_glib_minimal.cgo2.o -lglib-2.0
/usr/lib/go/pkg/tool/linux_amd64/cgo -objdir $WORK/command-line-arguments/_obj/
-dynimport $WORK/command-line-arguments/_obj/_cgo_.o -dynout
$WORK/command-line-arguments/_obj/_cgo_import.c
/usr/lib/go/pkg/tool/linux_amd64/6c -F -V -w -I $WORK/command-line-arguments/_obj/ -I
/usr/lib/go/pkg/linux_amd64 -o $WORK/command-line-arguments/_obj/_cgo_import.6 -D
GOOS_linux -D GOARCH_amd64 $WORK/command-line-arguments/_obj/_cgo_import.c
gcc -I . -g -O2 -fPIC -m64 -pthread -o $WORK/command-line-arguments/_obj/_all.o
$WORK/command-line-arguments/_obj/_cgo_export.o
$WORK/command-line-arguments/_obj/test_glib_minimal.cgo2.o -Wl,-r -nostdlib
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a
/usr/lib/go/pkg/tool/linux_amd64/6g -o $WORK/command-line-arguments/_obj/_go_.6 -p
command-line-arguments -D _/media/portable1/all1/projects/go-cgo -I $WORK
$WORK/command-line-arguments/_obj/_cgo_gotypes.go
$WORK/command-line-arguments/_obj/test_glib_minimal.cgo1.go
/usr/lib/go/pkg/tool/linux_amd64/pack grcP $WORK $WORK/command-line-arguments.a
$WORK/command-line-arguments/_obj/_go_.6 $WORK/command-line-arguments/_obj/_cgo_import.6
$WORK/command-line-arguments/_obj/_cgo_defun.6 $WORK/command-line-arguments/_obj/_all.o
cd .
/usr/lib/go/pkg/tool/linux_amd64/6l -o test_glib_minimal -L $WORK
$WORK/command-line-arguments.a

Running these finishes with:

/var/tmp/go-link-sViLsd/000000.o: In function `foo':
/media/portable1/all1/projects/go-cgo/test_glib_minimal.go:7: undefined reference to
`g_printf'
collect2: error: ld returned 1 exit status
/usr/lib/go/pkg/tool/linux_amd64/6l: running gcc failed: unsuccessful exit status 0x100

I'd expect that 'go build' shouldn't do anything implicitly, thus the output commands
are always enough to do the same, 'go build' does.

I've been using go1.2 from Debian. Near the same behavior is in tip. (Even worse, since
the output command for cmd/pack doesn't anymore correspond to the recent update to
cmd/pack.)

I've could find out, what is being done implicitly by go build. It scans the go files
and after it finds 
#cgo CFLAGS
#cgo LDFLAGS
it collects them. The CFLAGS are passed as command line arguments (can be seen above) to
the cmd/cgo, but not LDFLAGS. LDFLAGS are being put only into CGO_LDFLAGS environment
variable (where cgo looks for them), which is not reflected by commands and it might be
not a good solution.

Using gccgo compiler doesn't work either:
$ go build --compiler gccgo test_glib_minimal.go
# command-line-arguments
/media/portable1/all1/projects/go-cgo/test_glib_minimal.go:7: error: undefined reference
to 'g_printf'
collect2: error: ld returned 1 exit status

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions