visual-studiosdl-2static-linking

How do I statically link SDL2 in Visual Studio 2022?


How do I link SDL2 statically without having to have DDL's?

also I've read somewhere online that you have to statically build DDL's into lib libraries?


Solution

  • You will need to build SDL2 by yourself as a static library.


    First, clone the SDL Repo, preferably a stable tag, latest one being 2.28.1 at the time of writing.


    Then, open the SDL Solution located at <SDL Repo>/VisualC/SDL.sln.

    Once the solution has opened, right click the SDL2 project (on the right tab) and select Properties:

    Picture of the VS project being right clicked


    A new window will open, change Configuration to All configurations and Platform to All Platforms:

    Picture describing the above sentence


    Then, in Configuration Properties-> General, change Configuration Type from Dynamic Library (.dll) to Static library (.lib):

    Picture of the SDL2 Project properties with arrows to guide where to click


    Click OK to save your changes and close the window:

    Picture describing the above sentence


    Now, you can set the target platform of your choice and hit Build -> Build SDL2 to build SDL2 as a static library:

    . .

    Do NOT Build the entire solution, as it will build the test projects, which will fail.


    You will find built .lib files in the output folder that corresponds to your target configuration:

    .

    Do note that there won't be any DLL, as statically linking libraries using visual studio requires a .lib (and not a .dll, as those are for dynamic linking).


    Now, just follow the usual installation tutorial for VS (that normally makes you dynamically link SDL2), but copy SDL2.lib, SDL2main.lib and SDL2test.lib over the ones found in the dynamic link package.

    You can then remove the DLLs.

    Do note that for your project to build, you will need to statically link winmm.lib, setupapi.lib, version.lib and Imm32.lib to it.

    For this, open the save property window as you opened earlier, but for your project that uses SDL2, and go in Configuration Properties -> Linker -> Input and add the aforementionned filenames into Additional Dependencies.

    Your project should now build, with a static SDL2 included.


    Note: If you use SDL2 modules like SDL2_image, SDL2_ttf, SDL2_mixer, etc, you will need to statically build them as well, using the same method.