Building SDL2 on Windows with VS 2019

Building SDL2 on Windows with VS 2019

I used to work with Visual Studio 2017 when I was really doing some development on Windows 10 (my hobby projects are primarily programmed on my Linux machine, but I want to make my software Windows-compatible, if possible).

Now after I have switched my build environment from Visual Studio 2017 to the 2019 version, I was unable to build the OGRE 3D engine – or to be more precise: the SDL2 part of it:

[143/144] Linking C shared library SDL2.dll
FAILED: SDL2.dll SDL2.lib
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_dll --intdir=CMakeFiles\SDL2.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\mt.exe --manifests  -- C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\SDL2.rsp  /out:SDL2.dll /implib:SDL2.lib /pdb:SDL2.pdb /dll /version:2.0 /machine:x64 /INCREMENTAL:NO /NODEFAULTLIB   && cd ."
LINK: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\SDL2.rsp /out:SDL2.dll /implib:SDL2.lib /pdb:SDL2.pdb /dll /version:2.0 /machine:x64 /INCREMENTAL:NO /NODEFAULTLIB /MANIFEST /MANIFESTFILE:SDL2.dll.manifest" failed (exit code 1120) with the following output:
Creating library SDL2.lib and object SDL2.exp
SDL_string.c.obj : error LNK2019: unresolved external symbol memset referenced in function SDL_vsnprintf_REAL
SDL2.dll : fatal error LNK1120: 1 unresolved externals
ninja: build stopped: subcommand failed.

I personally do most of my Windows builds with CMake + Ninja so I don't have to hassle with any Visual Studio UI stuff. But as you can see here, VC++ is unable create the final library as the linker is missing a external symbol in function SDL_vsnprintf_REAL. The same command works with an Visual Studio 2017 environment, though.

After some investigation I found out that the library vcruntime.lib is missing but is somehow required for VC++ 2019 builds. I did not deep dive into it and thus I have not yet exact details why this specific dependency is now required with the 2019 version of the compiler. But there is a solution: Just add the vcruntime library to the list of extra libraries to be linked with the SDL2 project. Therefore, I only have added the keyword vcruntime to the list of Win32 native libraries to be linked with SDL2 in CMakeLists.txt:

# Libraries for Win32 native and MinGW
list(APPEND EXTRA_LIBS vcruntime user32 gdi32 winmm imm32 ole32 oleaut32 version uuid)

Right after that I have just restarted the build and voila – it worked:

[143/144] Linking C shared library SDL2.dll
Creating library SDL2.lib and object SDL2.exp
Show Comments