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: cross-compiling a darwin plugin fails to link #36018

Closed
kstenerud opened this issue Dec 6, 2019 · 3 comments
Closed

cmd/go: cross-compiling a darwin plugin fails to link #36018

kstenerud opened this issue Dec 6, 2019 · 3 comments

Comments

@kstenerud
Copy link

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

$ go version
go version go1.13.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/karl/.cache/go-build"
GOENV="/home/karl/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/karl/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/karl/tmp/tgo/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build277086046=/tmp/go-build -gno-record-gcc-switches"

Description of problem

$ cat go.mod 
module tgo

go 1.13
$ cat main.go 
package main

func main() {
}
$ GOOS=linux GOARCH=amd64 go build -buildmode=plugin
$ GOOS=darwin GOARCH=amd64 go build -buildmode=plugin
# tgo
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: -f may not be used without -shared
collect2: error: ld returned 1 exit status
@cagedmantis cagedmantis changed the title Cross-compiling a darwin plugin fails to link cmd/go: cross-compiling a darwin plugin fails to link Dec 6, 2019
@ianlancetaylor
Copy link
Contributor

When cross-compiling a plugin to Darwin you must set the environment variable CC to a Darwin cross-compiler. Building a plugin requires invoking the system linker.

@kstenerud
Copy link
Author

OK... Where can I get information about this? https://github.com/golang/go/wiki/WindowsCrossCompiling says that I just set GOOS and GOARCH and everything will work.

What do I put in CC? What counts as a Darwin cross-compiler? Why does invoking the system linker change things? Why does it spit out an error that makes it look like go itself has a bug rather than an informative message?

@ianlancetaylor
Copy link
Contributor

That page needs an overhaul. I added a few words.

Cross compiling a pure Go program works as described on that page. But cross-compiling a program that uses cgo, or that uses the build modes c-shared, c-archive, shared, or plugin, requires a C cross-compiler. A cross-compiler is a compiler that runs on your host system (in your case, GNU/Linux) and can compile a C program into a compiler that runs on your target system (in your case, macOS).

Invoking the system linker changes things because you need to use a linker that can produce an executable that runs on the target system.

I agree that the error message could likely be improved. That is #16395.

@golang golang locked and limited conversation to collaborators Dec 6, 2020
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