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

mac->linux cross compiling fails on linking "unknown option: --no-undefined" #462

Open
bartgrantham opened this issue May 23, 2020 · 3 comments

Comments

@bartgrantham
Copy link

I'm having trouble cross-compiling from mac (darwin) to linux, both amd64. I am running go version go1.14.3 darwin/amd64.

The program in question can be statically compiled for macOS (compiler host), and statically cross-compiled from macOS to Windows. But static cross-compiling from macOS to linux gives the following error during linking:

CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w -v" emulator.go
# github.com/veandco/go-sdl2/sdl
ld: unknown option: --no-undefined

I've read elsewhere that this is a gcc/clang difference, and I can see that the sdl sources include that flag for building linux binaries, but I don't know how to either tell it to use the gcc linker or switch that flag for something clang-appropriate.

Also, when I statically compile on linux for linux:

$ CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w" emulator.go
# github.com/veandco/go-sdl2/sdl
/usr/bin/ld: ../../../go/src/github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a(SDL.c.o): unable to initialize decompress status for section .debug_info
/usr/bin/ld: ../../../go/src/github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a(SDL.c.o): unable to initialize decompress status for section .debug_info
/usr/bin/ld: ../../../go/src/github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a(SDL_dynapi.c.o): unable to initialize decompress status for section .debug_info
/usr/bin/ld: ../../../go/src/github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a(SDL_dynapi.c.o): unable to initialize decompress status for section .debug_info
../../../go/src/github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a: error adding symbols: File format not recognized
collect2: error: ld returned 1 exit status

Dynamic builds under mac->mac and linux->linux work fine.

It might be something silly, I admit that I haven't dug too deep into the options here. Thanks for any help you can provide!

PS - I really like this project... I sure hope I can get a completely cross-platform build process working.

@gen2brain
Copy link
Collaborator

To cross-compile something that uses CGO you need to have a toolchain and compiler for that platform installed. For example, CC=x86_64-w64-mingw32-gcc when you cross compile for Windows, or CC=arm-linux-androideabi-clang for Android etc.

It seems you are just trying to add GOOS=linux and GOARCH=amd64 with your native compiler?

@bartgrantham
Copy link
Author

Now that you say this (and after sleeping on it), this makes perfect sense. I did install x86_64-w64-mingw32-gcc in order to have a Windows cross-compiler, but I haven't installed one for Linux. I guess part of why I was confused is because I was mixing up my native macOS gcc (which I have installed) and a Linux gcc cross-compiler (which I do not).

Is there a homebrew package for linux-gcc? Is x86_64-elf-gcc what I'm looking for here?

@Schobers
Copy link
Contributor

Schobers commented May 25, 2020

Got the same issue on Linux using (no cross-compilation here):

  • go1.14.3 linux/amd64
  • gcc 7.5.0 AND gcc 8.4.0

The previous libraries (with go1.13.8 & gcc 7.5.0) didn't have this issue. Output when go build-ing (-tags static):

# github.com/veandco/go-sdl2/sdl
/usr/bin/ld: ../../../../github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a(SDL.c.o): unable to initialize decompress status for section .debug_info
/usr/bin/ld: ../../../../github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a(SDL.c.o): unable to initialize decompress status for section .debug_info
/usr/bin/ld: ../../../../github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a(SDL_dynapi.c.o): unable to initialize decompress status for section .debug_info
/usr/bin/ld: ../../../../github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a(SDL_dynapi.c.o): unable to initialize decompress status for section .debug_info
../../../../github.com/veandco/go-sdl2/sdl/../.go-sdl2-libs/libSDL2_linux_amd64.a: error adding symbols: File format not recognized
collect2: error: ld returned 1 exit status

After some searching (edit):

The issue might be caused by a bug in elfutils & binutils.

  • binutils 2.31 & elfutils 0.174 is a OK combination. elfutils 0.176 broke with binutils <2.32. binutils 2.32 fixed compatibility again with elfutils 0.176 (and before I assume).

Since I'm using binutils 2.30 I assume a recent elfutils is used for building the static libraries (0.176 or more recent).

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants