Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

building SDL as a static library with static runtime doesn't compile/link with visual studio #846

SDLBugzilla opened this issue Feb 10, 2021 · 0 comments


Copy link

SDLBugzilla commented Feb 10, 2021

This bug report was migrated from our old Bugzilla tracker.

These attachments are available in the static archive:

Reported in version: HG 2.0
Reported for operating system, platform: Windows 7, x86

Comments on the original bug report:

On 2013-04-23 20:41:00 +0000, Wouter van Oortmerssen wrote:

I'm building SDL as a static library instead of a DLL, and I am using the static runtimes (/MT /MTd). This appears to conflict with SDL's LIBC replacements and gives a lot of errors when trying to link the resulting .lib against my main application.

I understand that you may prefer DLL builds and DLL runtimes, but to simplify deployment (and avoid WinSxS problems) I build everything statically. I managed to hack around most errors (primarily by commenting out HAVE_LIBC HAVE_ITOA HAVE_STRTOLL HAVE_SSCANF HAVE_M_PI HAVE_COPYSIGN HAVE_SCALBN in the windows config header before building the lib), but having at least a #define or whatever to make SDL compile/link correctly out of the box would be helpful.

On 2013-07-12 22:15:52 +0000, Ryan C. Gordon wrote:

(Sorry if you get a lot of copies of this email, we're touching dozens of bug reports right now.)

Tagging a bunch of bugs as target-2.0.0, Priority 2.

This means we're in the final stretch for an official SDL 2.0.0 release! These are the bugs we really want to fix before shipping if humanly possible.

That being said, we don't promise to fix them because of this tag, we just want to make sure we don't forget to deal with them before we bless a final 2.0.0 release, and generally be organized about what we're aiming to ship.

Hopefully you'll hear more about this bug soon. If you have more information (including "this got fixed at some point, nevermind"), we would love to have you come add more information to the bug report when you have a moment.


On 2013-07-27 06:04:35 +0000, Sam Lantinga wrote:

I don't build in this configuration, but if you submit a tested patch to easily switch to static builds I'll be happy to include it.


On 2013-07-27 12:17:49 +0000, Wouter van Oortmerssen wrote:

I could submit a patch, which would basically consist of an #ifdef around the defines I mention in my original post. But I am afraid that is a hack, since I don't know enough about the SDL internals to know if this change makes sense, or there is something more obvious going wrong elsewhere. If you just try to build in VS statically, you might be able to see why it is not compiling.

On 2013-07-27 12:23:25 +0000, Wouter van Oortmerssen wrote:

Also, I don't use Mercurial, I may not have time to set this up any time soon.

On 2013-07-28 14:30:59 +0000, Sam Lantinga wrote:

Okay, I'll take a look at this sometime after the 2.0 release.


On 2013-08-14 16:08:42 +0000, Joshua Granick wrote:

I very nearly have SDL2 working as a static library. I am currently using CMake with "-DDIRECTX=OFF" set, and there are only a few modifications required.

First, I need "/MT" instead of "/MD" as well, which I had trouble automating through CMake. As a result, I temporarily have modified it myself, changing "MultiThreadedDLL" to "MultiThreaded" and "MultiThreadedDebugDLL" to "MultiThreadedDebug" in the vcxproj file.

Second, there are two symbols defined by /stdlib/SDL_stdlib.c which create a conflict in my project. These are "_fltused" and "_ftol2_sse", which I temporarily commented out. Setting HAVE_LIBC caused a host of errors for me.

Third, I needed additional libs linked in my DLL. These include: "winmm.lib", "imm32.lib", "ole32.lib", "oleaut32.lib", and "version.lib", in addition to "gdi32.lib", "opengl32.lib", "user32.lib", "wldap32.lib" and "shell32.lib" which were already linked. Now one problem remains.

I have one problem when linking:

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

What would be the fix?

On 2013-08-14 17:03:13 +0000, Gabriel Jacobo wrote:

Joshua, you need to link in SDL_main probably.

Also, one of our GSOC projects may interest you, and feedback there is always appreciated.

On 2013-08-14 17:14:50 +0000, Joshua Granick wrote:

I am trying to link SDL2 into another static library, I'm sorry that I didn't mention this. As a result, I would expect that there should be no reference to "main" yet, since it isn't an application, but a library?

On 2013-09-23 18:28:05 +0000, CD-i Fan wrote:

I have SDL 2.0.0 building and as a static library. Everything seems to work so far, but I'm just starting to work with SDL...

I used the following steps:

  1. Ensuring that HAVE_LIBC is defined to avoid link conflicts with the standard library. I'm doing that via the project configuration.

  2. Uncommenting the following #define's in SDL_config_windows.h:


This is with Visual Studio 2010 Professional, in particular step 2 may be different depending on VS version.

I'm using /MT.

On 2013-09-28 13:49:37 +0000, wrote:

Created attachment 1341
Adds a condition so only the MSVC 2012 compiler defines the macros for the functions of its version.

Attaching a patch that adds a condition so that the HAVE_X supported by MSVC 2012 only get defined with that compiler. MSVC 2008 and 2010 will then build without any modification to the SDL source code.

Also moved HAVE_M_PI to a separate check. The Microsoft headers require _USE_MATH_DEFINES to be defined before they define the constants.

On 2013-09-28 17:31:47 +0000, Sam Lantinga wrote:

Your patch looks good, thanks!

On 2013-10-12 00:46:12 +0000, wrote:

Created attachment 1365
Fixes the version check for some functions that are only present with the MSVC 2013 CRT libraries.

I did my testing wrong and failed to see that 2012 doesn't have these functions. Microsoft implemented them in their upcoming 2013 version, though. The attached patch changes it to the check for the next version.

I also removed the HAVE_ITOA because that would require linking with oldnames.lib and it's easier to just let the SDL implementation take over.

On 2013-10-14 02:52:28 +0000, Sam Lantinga wrote:

Fixed, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

1 participant