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: GOOS=android requires explicitly setting CGO_ENABLED=1 #30216

Closed
saleemrashid opened this issue Feb 13, 2019 · 6 comments
Closed

cmd/go: GOOS=android requires explicitly setting CGO_ENABLED=1 #30216

saleemrashid opened this issue Feb 13, 2019 · 6 comments
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@saleemrashid
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.11.5 linux/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ GOOS=android GOARCH=arm GOARM=7 CC=arm-linux-androideabi-gcc CXX=false go env
GOARCH="arm"
GOBIN=""
GOCACHE="/home/saleem/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="android"
GOPATH="/home/saleem/.local/lib/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
GOARM="7"
CC="arm-linux-androideabi-gcc"
CXX="false"
CGO_ENABLED="0"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -marm -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build669201380=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello, World!")
}

What did you expect to see?

The command should succeed without explicitly setting CGO_ENABLED=1

What did you see instead?

Without explicitly setting CGO_ENABLED (go env shows it defaults to CGO_ENABLED=0)

$ GOOS=android GOARCH=arm GOARM=7 CC=arm-linux-androideabi-gcc CXX=false go build
/usr/lib/go/pkg/tool/linux_amd64/link: running arm-linux-androideabi-gcc failed: exit status 1
/tmp/go-link-934544495/go.o:go.go:_cgo_init: error: undefined reference to 'x_cgo_init'
/tmp/go-link-934544495/go.o:go.go:_cgo_notify_runtime_init_done: error: undefined reference to 'x_cgo_notify_runtime_init_done'
/tmp/go-link-934544495/go.o:go.go:_cgo_thread_start: error: undefined reference to 'x_cgo_thread_start'
/tmp/go-link-934544495/go.o:go.go:runtime._cgo_setenv: error: undefined reference to 'x_cgo_setenv'
/tmp/go-link-934544495/go.o:go.go:runtime._cgo_unsetenv: error: undefined reference to 'x_cgo_unsetenv'
/tmp/go-link-934544495/go.o:go.go:runtime.cgo_yield: error: undefined reference to '_cgo_yield'
collect2: error: ld returned 1 exit status

Explicitly setting CGO_ENABLED=1 leads to a successful build

$ GOOS=android GOARCH=arm GOARM=7 CC=arm-linux-androideabi-gcc CXX=false CGO_ENABLED=1 go build
$ 
@saleemrashid
Copy link
Author

See also #25963

@bcmills bcmills added the mobile Android, iOS, and x/mobile label Feb 13, 2019
@bcmills
Copy link
Contributor

bcmills commented Feb 13, 2019

CC @eliasnaur @ianlancetaylor

@bcmills bcmills added this to the Go1.13 milestone Feb 13, 2019
@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 13, 2019
@eliasnaur
Copy link
Contributor

The go tool disables CGO_ENABLED when cross-compiling:

$ go env CGO_ENABLED
1
$ GOOS=android go env CGO_ENABLED
0

I suppose we could keep CGO_ENABLED when CC or CXX is set?

@ianlancetaylor ianlancetaylor changed the title GOOS=android requires explicitly setting CGO_ENABLED=1 cmd/go: GOOS=android requires explicitly setting CGO_ENABLED=1 Feb 13, 2019
@ianlancetaylor
Copy link
Contributor

It would be nice to figure out when we can safely set CGO_ENABLED, but I'm not sure it should be simply because CC is set. We have a corresponding set of bug reports from people who set CC to the wrong compiler. But I don't have a better suggestion.

@eliasnaur
Copy link
Contributor

How about keeping CGO_ENABLED for geese that don't support internal linking?

@rsc
Copy link
Contributor

rsc commented Apr 12, 2019

We have a very simple rule right now: cgo is disabled for all cross-compiles. We should probably keep that simple rule. People building for Android have needed to set CGO_ENABLED from the start, and they seem to be coping just fine.

@rsc rsc closed this as completed Apr 12, 2019
@golang golang locked and limited conversation to collaborators Apr 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants