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/link: cannot cross compile a linux plugin on macOS #22462

Open
ash2k opened this issue Oct 27, 2017 · 18 comments
Open

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

ash2k opened this issue Oct 27, 2017 · 18 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@ash2k
Copy link
Contributor

ash2k commented Oct 27, 2017

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
Copy link

ghost commented Oct 27, 2017

#12888

@bronze1man
Copy link
Contributor

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

ghost commented Oct 27, 2017

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
Copy link

ghost commented Oct 27, 2017

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

You need to have a cross-compiler.

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

@ianlancetaylor ianlancetaylor added this to the Go1.10 milestone Oct 27, 2017
@ghost
Copy link

ghost commented Oct 27, 2017

CC @ALTree

@ALTree
Copy link
Member

ALTree commented Oct 27, 2017

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

ghost commented Oct 27, 2017

FWIW, possibly #22041 could also use a HelpWanted label.

@bronze1man
Copy link
Contributor

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

davecheney commented Oct 28, 2017 via email

@ghost
Copy link

ghost commented Oct 28, 2017

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

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
Copy link

ghost commented Oct 29, 2017

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

The FrozenDueToAge #12888 having mentioned cross-compiling to Windows.

@mrhoric
Copy link

mrhoric commented Nov 11, 2017

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

ghost commented Nov 11, 2017

@mrhoric the last line of the shell output from #22462 (comment) indicates that crosstool-NG was utilised

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017
@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 23, 2018
@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Unplanned Jun 23, 2018
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@ahmetozer
Copy link

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
Copy link

LuBingtan commented Oct 21, 2022

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

@adonespitogo
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests