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 219 forks source link

Include static libraries in project? #329

Closed gen2brain closed 6 years ago

gen2brain commented 6 years ago

I have a couple of cgo projects where I include all .c source files, like here https://github.com/gen2brain/aac-go/blob/master/aacenc/aacenc_cgo.go or here https://github.com/gen2brain/x264-go/blob/master/x264c/x264c_cgo.go .

Compiler sees that as one file, and that probably cannot be done for all C projects, i.e. duplicate defines etc., I guess it is not easy for SDL, but there is also another method, with -L${SRCDIR}/libs. I have done that for this project https://github.com/gen2brain/go-fitz/blob/master/fitz_cgo.go , where I include static libraries cross compiled for Windows, Linux, macOS, RPi etc.

That I think can be a good addition for go-sdl2, if you also include header files, there will be no requirements at all and binary doesn't link to dynamic libraries then. It can be available only with some build tag, doesn't have to be default. What do you think?

veeableful commented 6 years ago

Hey @gen2brain! It seems like a good addition to the project. We could make another repository to contain the libraries and have it as sub-module of go-sdl2 so we don't increase the repository size so much. The build tags will help to make it non-default approach of building programs based of go-sdl2. Do you think you could duplicate the setup on your project here?

gen2brain commented 6 years ago

Ok, I added some changes and static libraries here https://github.com/gen2brain/go-sdl2 . They are not configured for sub-module, currently you can just go get -tags static and then later go build -tags static. sdl2_mixer is compiled with libmpg123 support (version 2.0.3 of mixer is needed).

I tested some of examples in Linux, Windows and macOS and it works ok and binary is standalone.

In Linux, when old gcc is used it can probably return error recompile with -fPIC, not sure how to handle that. For some other project I prepared two libraries for old and new gcc, probably there is a better way.

veeableful commented 6 years ago

Nice! I tried compiling on my Arch system and running it on a Ubuntu 14.04 virtual machine and it seems to work! I couldn't seem to compile it on Ubuntu 14.04 (log here if you're interested) but I suppose that's a minor issue since this is a new addition and we can always improve it.

Anyway, I'll happily merge it if you make a PR!

gen2brain commented 6 years ago

I sent a PR. That error on Ubuntu 14 is what I meant, just found that it is not related to gcc or glibc, but binutils. It seems older versions then 2.26 don't know about that relocation, some info is here https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=808181 and https://stackoverflow.com/questions/46058050/unable-to-compile-unrecognized-relocation .

gen2brain commented 6 years ago

I added Android and RPi libs to https://github.com/veandco/go-sdl2-libs . After https://github.com/veandco/go-sdl2/pull/332 is merged I will also add flags for android/arm and linux/arm to *cgo_static.go files.

Binary can then be built with compiler from NDK, e.g:

CGO_ENABLED=1 CC=arm-linux-androideabi-gcc GOOS=android GOARCH=arm go build -tags static -ldflags "-s -w"

Although, such binary is not much useful on Android. I will see if I can combine .aar from gomobile, SDLActivity.java and gradle build. I will first update https://github.com/gen2brain/go-sdl2-android-example , and if all is good that example can be moved to .go-sdl2-examples.

Edit: Libraries are stripped, as much as possible to not break anything. Now, with Android and RPi libs included, whole repo is 30M.