Search code examples
c++csdl

Static linking of SDL2 2.0.18 with VS 2019, (memcpy already defined bug MT setting.)


I tried to compile and link a very simple SDL2 example code. It works for all the following configurations:

  • Win32 release and debug for both MD and MT setting in runtime lib.
  • x64 release and debug with runtime lib \MD

When I compile and link with x64, release and \MT I get this error:

Error LNK2005 memcpy already defined in SDL2.lib(SDL_stdlib.obj) c:\SDL2\libcruntime.lib
(memcpy.obj)

This is the same problem kind of.

https://github.com/libsdl-org/SDL/issues/3662

Below is my c++ compiler command line: /permissive- /ifcOutput "x64 Release" /GS /GL /W3 /Gy /Zc:wchar_t /Zi /Gm- /O2 /sdl /Fd"x64 Release\vc142.pdb" /Zc:inline /fp:precise /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MT /FC /Fa"x64 Release" /EHsc /nologo /Fo"x64 Release" /Fp"x64 Release\SDL2.pch" /diagnostics:column

Here are the linker command line: /OUT:"C:\Users\jerry\source\repos\SDL2\x64 Release\SDL2.exe" /MANIFEST /LTCG:incremental /NXCOMPAT /PDB:"C:\Users\jerry\source\repos\SDL2\x64 Release\SDL2.pdb" /DYNAMICBASE "SDL2main.lib" "zlib.lib" "libpng16.lib" "libwebp.lib" "jpeg.lib" "SDL2_image.lib" "SDL2.lib" "winmm.lib" "imm32.lib" "version.lib" "Setupapi.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X64 /OPT:REF /INCREMENTAL:NO /PGD:"C:\Users\jerry\source\repos\SDL2\x64 Release\SDL2.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"x64 Release\SDL2.exe.intermediate.manifest" /LTCGOUT:"x64 Release\SDL2.iobj" /OPT:ICF /ERRORREPORT:PROMPT /ILK:"x64 Release\SDL2.ilk" /NOLOGO /LIBPATH:"D:\SDL2Lib\x64" /TLBID:1

So how to make this work with VS 2019 and x64 and \MT setting? can pragma guards work in someway. What I learned is that the optimizer is doing memcpy and other things.


Solution

  • Thanks to keltar advice about the SDL_LIBC flag

    Solution is in the file SDL_config.h change the following starting from row 32

    #if defined(__WIN32__)
    #include "SDL_config_windows.h"
    #elif defined(__WINRT__)
    ....
    

    and change it to this

    #if defined(__WIN32__)
    #if defined(_WIN64)
    #define HAVE_LIBC 1
    #endif
    #include "SDL_config_windows.h"
    #elif defined(__WINRT__)
    ....