Official webpage says: “SDL 2.0, unlike 1.2, uses the zlib license, which means you can build a static library linked directly to your program, or just compile SDL’s C code directly as part of your project”. I didn’t find any guides how to do this. So I just tried to compile SDL sources alongside my project’s sources. But when I try to write simple hello world programm Cmake produces linker error: " main.cxx.obj:main.cxx:(.text+0x31): undefined reference to `SDL_Init’ ".
As I said, I found no examples of doing this, so what’s the right way?
While it isn’t recommended at all ( https://github.com/spurious/SDL-mirror/blob/0df1fc13af7bf1a36b2c33c422a4d7bd54e5a38c/src/dynapi/SDL_dynapi.h#L25 ), you should be able to statically link SDL just add whole sources ( https://github.com/okuoku/sdl2-static-cmake/blob/aa465f9e8745c12a71bad6f09c33efe0bab9e988/sdl2-srcs.cmake ), define proper C definitions for the platform you target ( https://github.com/okuoku/sdl2-static-cmake/blob/aa465f9e8745c12a71bad6f09c33efe0bab9e988/CMakeLists.txt#L74 ) and disable dynapi ( https://github.com/okuoku/sdl2-static-cmake/blob/aa465f9e8745c12a71bad6f09c33efe0bab9e988/CMakeLists.txt#L28 ).
Anyway, there’s no right-way and it’s a somewhat hard-way. So don’t try to do this unless you’re enough familiar with SDL internals… I guess almost noone using SDL in such way.
If you really think you must link statically, at least don’t disable dynapi.
I think the easiest way to link SDL2 statically is to build it as a static lib (.a on unix-likes, .lib on Windows), using the normal SDL buildsystem, and link against that.
I think building SDL2 as part of your build is a huge pain in the ass, as you’ll have to figure out which source files are needed on what platform and what definitions to set etc
Thank you for your answer! Your github project is suitable for win32 apps. Is there any CMakeLists.txt file examples, supporting android and ios apps?
I’m going to create some really lightweight crossplatform apps, so I need some static linkage. And I’m reaaaaaaaaaly afraid of the official android app building guide. I just want some .a lib files to link in my already made Native Activity Android.mk file.
Thank you for your answer! I’m going to create some really lightweight crossplatform apps, so I need some static linkage.
I decided not to use source files directly, but I just want some .a lib files to link in my already made Native Activity Android.mk file. Official android building guide describes only using built-in toolchain with JNI bindings. I don’t have any ideas how to correctly cross-compile SDL and SDL extensions to android arm ABIs.
Do you know how to implement it? Or/And ios compiling?
Is there any CMakeLists.txt file examples, supporting android and ios apps?
Nope. On Android, you will need to use shared library because the bundled Java bridge(SDLActivity.java) specifically assumes SDL was linked as shared library. FWIW, CMake project looks being prepared( https://bugzilla.libsdl.org/show_bug.cgi?id=3918 ).