veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.2k stars 218 forks source link

static build for linux produces dynamically linked executable #399

Open tenox7 opened 5 years ago

tenox7 commented 5 years ago
$ CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags '-s -w'  foo.go
$ ldd foo
    linux-vdso.so.1 (0x00007ffea0d18000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007effa257a000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007effa2376000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007effa2157000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007effa1f4f000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007effa1b5e000)
    /lib64/ld-linux-x86-64.so.2 (0x00007effa2918000)

I tried using -extldflags "-static" but that produces a binary that either crashes or hangs forever. It could be something to do with Alsa lib.

Would it be possible to support fully statically linked binaries?

veeableful commented 5 years ago

Hmm I'm not too sure, but perhaps it is possible if we compile SDL2 from source using musl-gcc?

tenox7 commented 5 years ago

Perhaps doesn't have to be with musl, but just static.

gcstr commented 3 years ago

I can still reproduce the issue.

$ CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w"

$ ldd guitest
        linux-vdso.so.1 (0x00007fffba277000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8355263000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f835525d000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f835523b000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8355051000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f83553da000)

Is there a way to compile with static libs?

veeableful commented 3 years ago

Hi @gcstr, I believe SDL2 is compiled with glibc which is why it can't be fully static. I could be wrong though.

Do you have a specific issue that is caused by this behavior?

UPDATE: It seems like SDL2 can be compiled statically. I will investigate how to accomplish that.

veeableful commented 3 years ago

Sorry, I couldn't figure out how to make it completely static. The sdl2-config --static-libs command still defines some dynamic libraries though they should be available on most Linux platform.

$ sdl2-config --static-libs
-L/usr/local/lib -lSDL2 -Wl,--no-undefined -lm -ldl -lpthread -lrt

If anyone knows how to make it completely static, please let me know!

gen2brain commented 3 years ago

@gcstr It is not really recommended to build a fully static binary with Glibc, you will get a big fat warning from Glibc, and you will have issues with Alsa, which is hard to compile statically (probably some patches are needed).

If you really want this, you need to use musl toolchain to build SDL and all dependencies fully static, and again apply some patches to Alsa (probably Alpine distro have some). Also, Musl recommends linking libC statically, with Glibc you get a warning not to do that.