Thank you!
With your help, I have now been able to get SDL2 to build statically for Windows. I have opted to continue to use CMake, because (eventually) I’d hope we’ll be able to build SDL2 without patches or modifications.
First, we added this to our CMakeLists.txt file, and define FORCE_STATIC_VCRT when compiling for Windows. This is roughly pulled from the OpenAL CMakeLists.txt file:
if(MSVC)
option(FORCE_STATIC_VCRT “Force /MT for static VC runtimes” OFF)
if(FORCE_STATIC_VCRT)
foreach(flag_var
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES “/MD”)
string(REGEX REPLACE “/MD” “/MT” ${flag_var} “${${flag_var}}”)
endif()
endforeach(flag_var)
endif()
endif(MSVC)
For Mac and Linux, EXTRA_CFLAGS was enough:
cd SDL2-2.0.0-build; cmake …/SDL2-2.0.0 -DSDL_STATIC=ON -DEXTRAA_CFLAGS=“-fPIC -m64”
cd SDL2-2.0.0-build; make SDL2-static
(we also -m32, of course, for 32-bit builds)
For Windows, we define the above, as well as flags Norfanin was using:
cd SDL2-2.0.0-build; cmake …/SDL2-2.0.0 -DDIRECTX=OFF -DSDL_STATIC=ON -DFORCE_STATIC_VCRT=ON -DEXTRA_CFLAGS=“-MT -Z7 -DSDL_MAIN_HANDLED -DWIN32 -DNDEBUG -D_CRT_SECURE_NO_WARNINGS -DHAVE_LIBC -D_USE_MATH_DEFINES -DUNICODE”
cp patches/SDL2/SDL_config_windows.h SDL2-2.0.0/include
cp patches/SDL2/SDL_stdinc.h SDL2-2.0.0/include
msbuild SDL2-2.0.0-build/SDL2-static.vcxproj /p:Configuration=Release
The patches are somewhat minimal. I had to add “SDL_config_windows.h”, <process.h> and <signal.h> in order to compile… I put it in “SDL_stdinc.h” but there’s better places to put it:
#ifdef WIN32
#include “SDL_config_windows.h”
#include <process.h>
#include <signal.h>
#endif
I used Norfanin’s “SDL_config_windows.h” with a few modifications. We are not building using the DirectX SDK (using our own audio and OpenGL), so I changed SDL_VIDEO_RENDER_D3D from 1 to 0, as well as SDL_AUDIO_DRIVER_DSOUND and SDL_AUDIO_DRIVER_XAUDIO2.
For now, I also disabled SDL_JOYSTICK_DINPUT to compile. Setting this to zero still resulting in missing symbols when linking. Commenting out the define resulted in success.
Perhaps it would not be too difficult to integrate some changes into the project, so it can compile statically out of the box for the next version?
Thank you again for all your hard work – I know how much it can take to build and support an open-source project.On Tue, 17 Sep 2013 06:58:05 -0700, Norfanin wrote:
Static linking works, but I highly recommend you use HAVE_LIBC and the
Visual C++ runtime library in this case. The SDL implementations for
the Visual C++ specific functions may cause problems otherwise.
If the attachment made it though, it contains a batch file (gmail
didn’t let me send it as a .bat. Just rename it.) and two C files that
might be similar to your situation. Download the SDL 2 source and
extract it beside those files. I also included the
SDL_config_windows.h for MSVC 2010 if you build with that. I think the
HAVE_X defines for HAVE_LIBC are tailored for 2012.
If you run build.bat in the build environment it should yield a DLL
with SDL 2 statically linked and an executable importing some
functions from it. I have some x86 specific options to suppress
warnings, but changing them over to x64 to make a 64-bit build should
work perfectly.
Other than that, it might be helpful if we can see how you fit things
together. cl.exe and link.exe invocations and all that.