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: Building application fails because invoking external linker doesn't honor CGO_LDFLAGS env variable #5205

Closed
ugorji opened this issue Apr 4, 2013 · 9 comments
Milestone

Comments

@ugorji
Copy link
Contributor

ugorji commented Apr 4, 2013

My application built successfully with just go install 

It depends on a package which uses cgo. Building that package works fine. However, when
I build the main package, it fails in a cryptic way.

From debugging, it fails because the env variable CFO_LDFLAGS is not passed to the
invocation of the external linker (ld). So ld cannot find the shared libraries my
cgo-pkg depends on.

I got this to work by explicitly passing 
-ldflags=-extldflags=-L${MYLIBDIR} to the go install call.

Note that this works fine in Go 1.0.3. 

So now I have something like this in my build script.

    case `go version` in
        go*1.0*) go install -a . ;;
        *) go install "-ldflags=-extldflags=-L${MYLIBDIR}" -a . ;;
    esac

Without passing the -ldflags -extldflags=-L..., error message looks like:

# app/blackannexdatastore
/usr/bin/ld: cannot find -lndb
/usr/bin/ld: cannot find -lleveldb
collect2: error: ld returned 1 exit status
crypto/tls.extensionNextProtoNeg: /opt/go-tip/pkg/tool/linux_amd64/6l: running gcc
failed: unsuccessful exit status 0x100

I ran passing -v flags to -ldflags to verify that the CGO_LDFLAGS env variable wasn't
passed to host linker.

    host link: gcc -m64 -gdwarf-2 -o $WORK/app/blackannexdatastore/_obj/exe/a.out -rdynamic /home/all/tmp/go-link-fwKtSe/000000.o /home/all/tmp/go-link-fwKtSe/000001.o /home/all/tmp/go-link-fwKtSe/000002.o /home/all/tmp/go-link-fwKtSe/go.o -lndb -lleveldb -lpthread <CGO_LDFLAGS should be passed here>

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g 

Which operating system are you using?
Linux 3.5.0-26-generic #42-Ubuntu SMP x86_64 GNU/Linux

Which version are you using?  (run 'go version')
go version devel +d646225cb410 Wed Apr 03 17:03:42 2013 -0700 linux/amd64

Please provide any additional information below.
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added priority-soon, go1.1, removed priority-triage.

@minux
Copy link
Member

minux commented Apr 4, 2013

Comment 2:

Status changed to Accepted.

@axw
Copy link
Contributor

axw commented Apr 7, 2013

Comment 3:

Ugorji, do you use "#cgo LDFLAGS:...", or are you setting CGO_LDFLAGS in the calling
environment? I do the latter in llgo (no pkg-config available, LDFLAGS are dynamic), and
I'm getting the same error.
Seems the problem is that cgo doesn't look for CGO_LDFLAGS, and so it gets missed when
the _cgo_flags file is written. This file is used for recording flags for external
linking.
I have a CL incoming which should fix that particular issue... but it wouldn't fix Issue
5224.

@ugorji
Copy link
Contributor Author

ugorji commented Apr 7, 2013

Comment 4:

Yes, I was setting $CGO_LDFLAGS (ie the environment variable).

@minux
Copy link
Member

minux commented Apr 8, 2013

Comment 5:

This issue was closed by revision afb49aa.

Status changed to Fixed.

@gopherbot
Copy link

Comment 6 by manoj.dayaram@moovweb.com:

I'm currently working off the tip (and also tried this on the latest release candidate)
and I still get this issue.
As per suggested by the original issue, I tried running
go build -ldflags -extldflags=-L/path/to/my/libs
And that worked fine, but running a normal go build gave me the same error:
/usr/bin/ld: cannot find -lsass
/usr/bin/ld: cannot find -lonig
collect2: ld returned 1 exit status
/home/noj/.gvm/gos/gotip/pkg/tool/linux_amd64/6l: running gcc failed: unsuccessful exit
status 0x100
Is this change not represented in the latest tip or go1.1rc2?  If so, will it be
available for go1.1?
Thanks.

@davecheney
Copy link
Contributor

Comment 7:

This was closed on the 9th of April, close to a month ago. It has been part of both rc
candidates. 
Can you please provide a working code sample to demonstrate the failure you are seeing.

@gopherbot
Copy link

Comment 8 by manoj.dayaram@moovweb.com:

Ah, I think I figured it out.  It seems it sometimes works and sometimes doesn't.
When I run "go build" from a directory inside the GOPATH path, then it works fine.
If I run it outside of the GOPATH path, then it fails.
I will open up a separate issue for this, thank you for your help.

@gopherbot
Copy link

Comment 9 by manoj.dayaram@moovweb.com:

I have created a new issue:
https://golang.org/issue/5428

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 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

7 participants