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

gccgo: Trouble cross-compiling to GOARCH=mips using gccgo #18942

Closed
elimisteve opened this issue Feb 4, 2017 · 15 comments
Closed

gccgo: Trouble cross-compiling to GOARCH=mips using gccgo #18942

elimisteve opened this issue Feb 4, 2017 · 15 comments
Milestone

Comments

@elimisteve
Copy link
Contributor

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

go version go1.8rc3 linux/amd64

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

linux/amd64

What did you do?

I tried cross-compiling a trivial "hello world" Go program -- namely https://github.com/elimisteve/hello/blob/master/hello.go -- with this command:

$ GOOS=linux GOARCH=mips go1.8rc3 build -x -compiler=gccgo .

What did you expect to see?

No output and a successfully-compiled 32-bit MIPS binary in my local directory.

What did you see instead?

$ GOOS=linux GOARCH=mips go1.8rc3 build -x -compiler=gccgo .
WORK=/tmp/go-build262885004
mkdir -p $WORK/github.com/elimisteve/hello/_obj/
mkdir -p $WORK/github.com/elimisteve/hello/_obj/exe/
cd /home/ubuntu/go/src/github.com/elimisteve/hello
/usr/bin/gccgo -I $WORK -c -g -mabi=32 -march=mips32 -fgo-relative-import-path=_/home/ubuntu/go/src/github.com/elimisteve/hello -o $WORK/github.com/elimisteve/hello/_obj/_go_.o ./hello.go
# github.com/elimisteve/hello
gccgo: error: unrecognized argument in option ‘-mabi=32’
gccgo: note: valid arguments to ‘-mabi=’ are: ms sysv

This is using gccgo 6, which installed 'libgo9as a dependency when I ranapt-get install gccgo`.

At https://tip.golang.org/doc/install/gccgo it says,

The GCC 6 releases include a complete implementation of the Go 1.6.1 user libraries. The Go 1.6 runtime is not fully merged, but that should not be visible to Go programs.

so I believe this is supposed to work? Thanks!

@cherrymui
Copy link
Member

It looks like your gccgo isn't the cross compiler, i.e. it's targeting the host (amd64) instead of mips. I don't know how to cross compile with gccgo though. You probably needs to set up some environment variable.

@cherrymui
Copy link
Member

cherrymui commented Feb 5, 2017

Maybe apt-get install gccgo-mips-linux-gnu?

@minux
Copy link
Member

minux commented Feb 5, 2017 via email

@elimisteve
Copy link
Contributor Author

@minux Well the key line seems to be

/usr/bin/gccgo -I $WORK -c -g -mabi=32 -march=mips32 -fgo-relative-import-path=_/home/ubuntu/go/src/github.com/elimisteve/hello -o $WORK/github.com/elimisteve/hello/_obj/go.o ./hello.go

which is using gccgo directly. Not sure if I should try xgo or whether building my own cross-compiling version of GCC, a la https://github.com/golang/go/wiki/GccgoCrossCompilation#build-the-cross-compiler, is necessary.

@minux
Copy link
Member

minux commented Feb 5, 2017 via email

@ianlancetaylor
Copy link
Contributor

Setting GOOS and GOARCH when running the go tool works to do cross-compiling with the gc toolchain, but it does not work when using the gccgo toolchain. It would be nice to make this work better, but it will never be as simple as it is with the gc toolchain.

@gopherbot gopherbot added this to the Gccgo milestone Mar 21, 2017
@jsw-davidhuang
Copy link

  1. sudo apt-get install gccgo-mipsel-linux-gnu
  2. mipsel-linux-gnu-gccgo -o main main.go

@adonespitogo
Copy link

This is my first time using gccgo and just wanting to cross-compile my go app for openwrt. Can anyone please tell me why importing non-standard package fails?

mipsel-linux-gnu-gccgo -o app ./main/main_mono.go
./main/main_mono.go:5:36: error: import file ‘github.com/my-private/package’ not found
    5 | import "github.com/my-private/package"

github.com/my-private/package is just arbitrary package name for confidentiality reasons. I am able to build this go program using standard go build command.

@cherrymui
Copy link
Member

@adonespitogo you're expected to use the go command that comes with gccgo, instead of the gccgo command directly. For cross-compilation, you might be able to do something like GOOS=linux GOARCH=mips GCCGO=mipsel-linux-gnu-gccgo CC=<your-C-cross-compiler> go build -compiler=gccgo

@adonespitogo
Copy link

Thanks for the advice @cherrymui . I tried CGO_ENABLED=0 GOOS=linux GOARCH=mips GCCGO=mipsel-linux-gnu-gccgo go build -tags="mono dev" -compiler=gccgo but now I have a different error:

package io/fs is not in GOROOT (/home/adonesp/.gvm/gos/go1.19.4/src/io/fs)

@ianlancetaylor
Copy link
Contributor

Make sure that you are using the go command that comes with gccgo, not the one that comes with the usual gc distribution.

Make sure that GOROOT is not set in the environment.

@adonespitogo
Copy link

My gccgo installation from ubuntu apt repository does not seem to have go command installed with it. I'm now compiling gcc go from the source. Will report back with the result.

@adonespitogo
Copy link

adonespitogo commented Mar 23, 2023

I've built gccgo from scratch following https://go.dev/doc/install/gccgo and now I have the go command from gccgo. Now I encounter another problem:

export GOOS=linux                                                                                                                                                                                
export GOARCH=mips                                                                                                                                                                               
export GCCGO=mipsel-linux-gnu-gccgo

/opt/gccgo/bin/go build -tags="mono dev" -compiler=/opt/gccgo/bin/gccgo -gccgoflags -Wl,-R,/opt/gccgo/lib64/ -static-libgo -o myapp.app main.go
/opt/gccgo/bin/go: error while loading shared libraries: libgo.so.21: cannot open shared object file: No such file or directory
❯ ls /opt/gccgo/lib64 | grep libgo.so
libgo.so
libgo.so.21
libgo.so.21.0.0

@adonespitogo
Copy link

It's working now, I guess. Here's my new env and build params:

export GOOS=linux
export GOARCH=mips
export GCCGO=/usr/bin/mips-linux-gnu-gccgo
export LD_LIBRARY_PATH=/opt/gccgo/lib64

/opt/gccgo/bin/go build -tags="mono dev" -compiler=gccgo -gccgoflags -Wl,-R,/opt/gccgo/lib64

Though I can't confirm it yet since I still have to update my go codes to use golang go1.18 instead of go1.19.

@ianlancetaylor
Copy link
Contributor

Thanks, closing for now.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Mar 23, 2023
@golang golang locked and limited conversation to collaborators Mar 22, 2024
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