Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gopherbot opened this issue Feb 3, 2014 · 8 comments
Closed
Milestone

Comments

@gopherbot
Copy link

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
@davecheney
Copy link
Contributor

Comment 1:

> 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.)
Could you please file a separate bug for the cmd/pack discrepancy. Thanks.

@gopherbot
Copy link
Author

Comment 2 by andrey.gursky@e-mail.ua:

Done: https://golang.org/issue/7262

@rsc
Copy link
Contributor

rsc commented Mar 3, 2014

Comment 3:

Labels changed: added release-go1.3maybe.

Status changed to Accepted.

@gopherbot
Copy link
Author

Comment 4:

Owner changed to @tul-project.

Status changed to Started.

@gopherbot
Copy link
Author

Comment 5:

This issue was closed by revision f081e2b.

Status changed to Fixed.

@gopherbot
Copy link
Author

Comment 6 by andrey.gursky@e-mail.ua:

Thanks.
Should I open a new issue for broken 'go build --compiler gccgo'? Or this depends on
another existing ticket?

@gopherbot
Copy link
Author

Comment 7:

Please open a new issue for gccgo if it is broken.

@gopherbot
Copy link
Author

Comment 8 by andrey.gursky@e-mail.ua:

Not needed anymore. It has been fixed on Mar 26
(https://golang.org/issue/7573).

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants