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

static build for linux produces dynamically linked executable

Open tenox7 opened this issue 6 years ago • 6 comments

$ 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?

tenox7 avatar Apr 09 '19 04:04 tenox7

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

veeableful avatar Apr 09 '19 14:04 veeableful

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

tenox7 avatar Apr 09 '19 17:04 tenox7

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?

gcstr avatar Dec 27 '20 20:12 gcstr

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 avatar Dec 28 '20 02:12 veeableful

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!

veeableful avatar Dec 28 '20 13:12 veeableful

@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.

gen2brain avatar Dec 30 '20 19:12 gen2brain