The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Static linking SDL2 for Windows
Joshua Granick
Guest

Although it is possible to statically link SDL 2 for Mac and Linux, I have not been able to find any resource where anyone got SDL 2 linking statically for Windows. We have done this for SDL 1.2 for a long time.

Here is the last error:

LIBCMT.lib(crt0.obj) : error LNK2019: unresolved external symbol _main reference
d in function ___tmainCRTStartup


We statically link SDL (and other dependencies) into a single dynamically loaded binary. Having an additional "main" defined won't work either... it will always throw:

runtime error R6030
- CRT not initialized


I believe this is caused by specific functions, which trigger the "CRT startup" behavior:

http://msdn.microsoft.com/en-us/library/xwhcs5z6(v=vs.80).aspx


For SDL 1.2, we compiled using "-DHAVE_LIBC", which does not appear to work with SDL2. I think (perhaps?) that this was the key that allowed us to compile SDL without triggering the CRT startup behavior, allowing static linking and bringing peace and harmony to the universe.

Is the "HAVE_LIBC" define known to have problems on SDL 2, currently? Is this an old, deprecated define?

Thank you so much for your help!
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Static linking SDL2 for Windows
Vittorio Giovara
Guest

On Tue, Sep 17, 2013 at 10:19 AM, Joshua Granick
wrote:
Quote:
Although it is possible to statically link SDL 2 for Mac and Linux, I have
not been able to find any resource where anyone got SDL 2 linking statically
for Windows. We have done this for SDL 1.2 for a long time.

Afaik SDL2 doesn't link with any c library at all and static linking
on windows has been somewhat broken.
Maybe this is useful to you https://bugzilla.libsdl.org/show_bug.cgi?id=1820

Vittorio
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Static linking SDL2 for Windows
Norfanin


Joined: 21 Jan 2012
Posts: 25
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.

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Static linking SDL2 for Windows
Joshua Granick
Guest

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:

Quote:
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.
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org