go-sdl2 icon indicating copy to clipboard operation
go-sdl2 copied to clipboard

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

Open bartgrantham opened this issue 4 years ago • 3 comments

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.

bartgrantham avatar May 23 '20 06:05 bartgrantham

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?

gen2brain avatar May 23 '20 10:05 gen2brain

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?

bartgrantham avatar May 23 '20 21:05 bartgrantham

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:

  • https://bugzilla.redhat.com/show_bug.cgi?id=1678204
  • https://bugs.launchpad.net/ubuntu/+source/binutils/+bug/1815774
  • https://bbs.archlinux.org/viewtopic.php?id=242682

Schobers avatar May 25 '20 20:05 Schobers