golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124k stars 17.67k forks source link

cmd/link: cannot cross compile a linux plugin on macOS #22462

Open ash2k opened 7 years ago

ash2k commented 7 years ago

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

go version go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

macOS Sierra 10.12.6 with latest Xcode 9.0.1 and Command Line Tools installed.

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/mmazurskiy/gopath"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/fh/7zp6kdcj3rgdd_7_nmw439_st0zyrt/T/go-build968645272=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

package main

func Process() {
}
GOOS=linux go build -buildmode=plugin -i -v -o build/bin/p1 github.com/atlassian/smith/cmd/p1
github.com/atlassian/smith/cmd/p1
# github.com/atlassian/smith/cmd/p1
warning: unable to find runtime/cgo.a
/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: unknown option: -z
clang: error: linker command failed with exit code 1 (use -v to see invocation)
GOOS=linux go build -buildmode=plugin -i -v -ldflags="-v" -o build/bin/p1 github.com/atlassian/smith/cmd/p1
github.com/atlassian/smith/cmd/p1
# github.com/atlassian/smith/cmd/p1
HEADER = -H4 -T0x401000 -D0x0 -R0x1000
searching for runtime.a in $WORK/runtime.a
searching for runtime.a in /usr/local/Cellar/go/1.9.2/libexec/pkg/linux_amd64_dynlink/runtime.a
searching for runtime/cgo.a in $WORK/runtime/cgo.a
searching for runtime/cgo.a in /usr/local/Cellar/go/1.9.2/libexec/pkg/linux_amd64_dynlink/runtime/cgo.a
warning: unable to find runtime/cgo.a
 0.00 deadcode
 0.01 pclntab=165547 bytes, funcdata total 31135 bytes
 0.01 dodata
 0.01 dwarf
 0.01 symsize = 0
 0.02 reloc
 0.02 asmb
 0.02 codeblk
 0.02 rodatblk
 0.02 datblk
 0.02 sym
 0.02 symsize = 22368
 0.02 symsize = 102000
 0.02 dwarf
 0.03 headr
 0.03 host link: "clang" "-m64" "-gdwarf-2" "-Wl,-z,relro" "-shared" "-Wl,-znow" "-Wl,-znocopyreloc" "-o" "/var/folders/fh/7zp6kdcj3rgdd_7_nmw439_st0zyrt/T/go-build232503875/github.com/atlassian/smith/cmd/p1/_obj/exe/a.out.so" "-rdynamic" "-Qunused-arguments" "/var/folders/fh/7zp6kdcj3rgdd_7_nmw439_st0zyrt/T/go-link-110686925/go.o"
/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: unknown option: -z
clang: error: linker command failed with exit code 1 (use -v to see invocation)

With explicit CGO_ENABLED=1 I get:

CGO_ENABLED=1 GOOS=linux go build -buildmode=plugin -i -v -ldflags="-v" -o build/bin/p1 github.com/atlassian/smith/cmd/p1
runtime/cgo
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What did you expect to see?

Successfully compiled plugin.

What did you see instead?

Failures.

ghost commented 7 years ago

https://github.com/golang/go/issues/12888

bronze1man commented 7 years ago

@forskning

I can cross compile a linux exe with cgo on macOS with go1.9 and gcc 7.2.0.

go version:

go version go1.9 darwin/amd64

gcc version:

> /usr/local/x86_64-ubuntu14.04-linux-gnu/bin/x86_64-ubuntu14.04-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=/usr/local/x86_64-ubuntu14.04-linux-gnu/bin/x86_64-ubuntu14.04-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/local/x86_64-ubuntu14.04-linux-gnu/bin/../libexec/gcc/x86_64-ubuntu14.04-linux-gnu/7.2.0/lto-wrapper
Target: x86_64-ubuntu14.04-linux-gnu
Configured with: /Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/src/gcc/configure --build=x86_64-build_apple-darwin16.7.0 --host=x86_64-build_apple-darwin16.7.0 --target=x86_64-ubuntu14.04-linux-gnu --prefix=/Volumes/ct-ng/ct-ng/x86_64-ubuntu14.04-linux-gnu --with-sysroot=/Volumes/ct-ng/ct-ng/x86_64-ubuntu14.04-linux-gnu/x86_64-ubuntu14.04-linux-gnu/sysroot --enable-languages=c,c++ --with-pkgversion='crosstool-NG crosstool-ng-1.23.0-220-g2ebbd3e3' --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --enable-libmpx --with-gmp=/Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/buildtools --with-mpfr=/Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/buildtools --with-mpc=/Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/buildtools --with-isl=/Volumes/ct-ng/build_dir/crosstool-ng/.build/x86_64-ubuntu14.04-linux-gnu/buildtools --enable-lto --enable-threads=posix --enable-target-optspace --disable-plugin --disable-nls --disable-multilib --with-local-prefix=/Volumes/ct-ng/ct-ng/x86_64-ubuntu14.04-linux-gnu/x86_64-ubuntu14.04-linux-gnu/sysroot --enable-long-long
Thread model: posix
gcc version 7.2.0 (crosstool-NG crosstool-ng-1.23.0-220-g2ebbd3e3)
ghost commented 7 years ago

The 10-10-15 dated comment by T. de Zeeuw in the link I provided seems to indicate a failed attempt to utilise a gcc compiled for darwin, rather than following I.L. Taylor's suggestion in an earlier comment to that issue, to utilise a "Darwin -> GNU/Linux cross-compiler".

ghost commented 7 years ago

https://github.com/golang/go/wiki/WindowsCrossCompiling

Perhaps similar as A. Donizetti's above wiki page it would be of interest to the community to have a "LinuxCrossCompiling" wiki page.

ianlancetaylor commented 7 years ago

You need to have a cross-compiler.

I guess I'll leave this issue open for documentation.

ghost commented 7 years ago

CC @ALTree

ALTree commented 7 years ago

@forskning I'm not really the author of that wikipage, I just happen to be the last person who edited it : )

That said, I agree we could use a {Linux or OsX}CrossCompiling wiki page, since it's something that apparently trips a lot of people and issues about this always end up with someone writing the usual "you need a cross-compiling toolchain to do this." We could write down the procedure nicely once, and then just link the wiki page.

ghost commented 7 years ago

FWIW, possibly https://github.com/golang/go/issues/22041 could also use a HelpWanted label.

bronze1man commented 7 years ago

Is it possible to just create a golang zip for macos/linux/windows that include a cross compile gcc for macos/linux/windows ? So that people will find that cross compile cgo just work,I do not want to install a lot of stuff to just make cross compile works.Just give me the working binary. I think the zip size is not a problem.The package for unity 3d is more than 5GB.

davecheney commented 7 years ago

Why don’t we just make the Go tool refuse to operate if it is in cross compilation mode and cgo is enabled and the host cc options are not set?

On 28 Oct 2017, at 00:48, Alberto Donizetti notifications@github.com wrote:

@forskning I'm not really the author of that wikipage, I just happen to be the last person who edited it : )

That said, I agree we could use {Linux,OsX}CrossCompiling wiki pages, since it's something that apparently trips a lot of people and issues about this always end up with someone writing the usual "you need a cross-compiling toolchain to do this." We could write it down nicely once and then just link the wiki page.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

ghost commented 7 years ago

https://forum.golangbridge.org/t/go-mobile-commands/7002

There's a recent somewhat lengthy series of topics on golangbridge.org.

Specifically pertaining to the subrepo mobile and external software; an install of Java8, Android Studio, and NDK, where just for the Android Studio/Android SDK there is the minimum requirement of an allocation of 2GB of free harddrive space.

I think its worth noting here the comment of I.L.T. to (the now Abandoned) https://golang.org/cl/69430, inferring that in regards to that proposed change in the golang.org introductory documentation, that installation of a cross-compiler was more intermediate or advanced level coding.

With host cc options not being set and a message output to that effect this might likewise infer that the coder was at an intermediate or advanced level, in order for that coder to interpret the shell message.

https://forum.golangbridge.org/t/cannot-find-package-golang-org-x-tools-cmd-goimports/7000

With all due respect I like S. Binet's comment near the closing of a separate golangbridge.org topic.

"it’s left as an exercize for the reader"

That is, with the documentation available, it's left to the reader to be able to locate that information.

But, I note the drawing of R. French on golang.org/help.

ianlancetaylor commented 7 years ago

Why don’t we just make the Go tool refuse to operate if it is in cross compilation mode and cgo is enabled and the host cc options are not set?

@davecheney works for me. Thanks.

ghost commented 7 years ago

@ash2k Do you ever have a need to build windows go programs on macOS?

The FrozenDueToAge https://github.com/golang/go/issues/12888 having mentioned cross-compiling to Windows.

mrhoric commented 7 years ago

@forskning As the issue #12888 saying:

In order to use cgo when cross-compiling, you need to set the CC environment variable to a Darwin -> GNU/Linux cross-compiler. You are using a Darwin native compiler, which can not work. Even if the build succeeded, the resulting code would not run on GNU/Lin

How do i get the Darwin -> GNU/Linux cross-compiler ? thx!

ghost commented 7 years ago

@mrhoric the last line of the shell output from https://github.com/golang/go/issues/22462#issuecomment-339882044 indicates that crosstool-NG was utilised

ahmetozer commented 2 years ago

Still not solved 😢


CGO_ENABLED=0

export GOOS=linux GOARCH=arm CGO_ENABLED=0                            
➜  allow_by_dns_query git:(master) ✗ go build -buildmode=plugin -trimpath  -o zabita_rule.so zabita_rule.go
# command-line-arguments
loadinternal: cannot find runtime/cgo
/opt/homebrew/Cellar/go/1.18.4/libexec/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
clang: error: invalid linker name in argument '-fuse-ld=gold'

CGO_ENABLED=1

➜  allow_by_dns_query git:(master) ✗ export GOOS=linux GOARCH=arm CGO_ENABLED=1                            
➜  allow_by_dns_query git:(master) ✗ go build -buildmode=plugin -trimpath  -o zabita_rule.so zabita_rule.go
# runtime/cgo
clang: error: argument unused during compilation: '-marm' [-Werror,-Wunused-command-line-argument]
GO111MODULE=""
GOARCH="arm"
GOBIN=""
GOCACHE="/Users/ahmet/Library/Caches/go-build"
GOENV="/Users/ahmet/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ahmet/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/Users/ahmet/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.18.4/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.18.4/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18.4"
GCCGO="gccgo"
GOARM="5"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ahmet/Desktop/ozer/zabita/go.mod"
GOWORK=""
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 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4v/6v36n2h552n4p33y4dz41f8c0000gq/T/go-build1389646958=/tmp/go-build -gno-record-gcc-switches"
LuBingtan commented 2 years ago

Still have this issue in golang 1.19 😭 Hope someone kindly fix this soon

adonespitogo commented 1 year ago

Base on the comments on related issues, I think we just need to set CGO_ENABLED=1 and CC=[your-compiler]. My problem is I don't know the executable name for this Mac OS cross-compiler. I've installed this using brew but I can't seem to make it work with:

CGO_ENABLED=1 CC=arm-linux-gnueabihf-cc GOOS=linux GOARCH=arm GOARM=7 go build --buildmode=plugin     

Anybody know what should be the value for CC=? env variable? I'm in Mac OS ventura apple silicon if that matters.