Linking SDL dynamically

Hello, i have linked my SDL program statically, but i would like to do that dynamically better. With statical linking, u just link SDL2.lib and SDL2main.lib and its all good. Meanwhile with dynamic linking, i havent figured it out yet, and i havent seen any solutions on the internet. I inserted SDL2.dll file to ouput executable folder, but it doesnt seem to work. I get a lot exernal unresolved symbols. I found on the internet that you need some sort of SDL2dll.lib, but i have not that in my sdl lib folder. I have just there 3 libs(SDL2.lib, SDL2main.lib and SDL2test.lib) For example with GLFW, you will get glfw3dll.lib. Iam sure it is trivial question for you, but i still cannot figure it out. Thank you very much for your help.

Where did you get SDL? For Windows the names from static and dynamic libraries differ from each other, second ones got __imp_ prefix, so you need special .lib file for them. You can generate it by hand.
For latest SDL2 (2.28.3) from Releases · libsdl-org/SDL · GitHub :

  1. Download this .def file and place it in same dir as SDL2.dll SDL2.def (17.1 KB)
  2. Open right Native Tools Command Prompt for VS 20XX (x86 or x64)
  3. Prompt this command: lib /def:SDL2.def /out:SDL2dll.lib
  4. Link this file to your project.

The .def file I provided here is generated by gendef tool for SDL2 version 2.28.3, not guaranteed to work with other versions, but likely it will because SDL2 has fairly stable api.

the doc for Visual Studio, docs\README-visualc.md in the SDL2 branch of the SDL GitHub repo, GitHub - libsdl-org/SDL at SDL2, worked perfectly for me with Visual Studio 2022:



[Edit: I am sorry, I managed to read your prompt wrong. The following instructions are for loading a *.dll file as a plugin from an already running SDL program. I’ll leave it here since I think it’s still interesting, but unfortunately it is not a solution in itself. I’ll make a new post below if I can find some helpful resources.]

I am on a different Operating System, but for me it was important to use absolute paths.
So that basically means that you need to select an install location such as C:\MyProgram\plugins and place the *.dll files in that folder. Or you can use SDL_GetBasePath() and set up the installation that way.

Then you can use

void * handle = SDL_LoadObject("C:\\MyProgram\plugins\mylibrary.dll");
if(!handle)
{
    // the file did not load correctly, double check that the path is correct.**
}

Then you need to load functions from inside that library object file

typedef void(*function_Ptr)(void);
function_Ptr myFunction = SDL_LoadFunction(handle, "MyFunctionName");

(Please note that function_Ptr needs to be a function pointer that has the correct prototype for the function you wish to load.

From here you just call myFunction() as if it were a regular function.

Don’t forget to SDL_UnloadObject(handle) when you are doing cleanup.

It is also possible to make the function that you are loading as a class spawner (AKA factory) that returns a dynamically created class object when called. (Just be aware that this makes debugging difficult. Every member declaration MUST have a definition. You should also have a destruction function inside the dll file so that destruction occurs in the same library.

Ultimately I set up to take plugin files from drag and drop events, copy them to my plugin folder, and kept a text file list of plugins for future use.

Sorry for my previous unhelpful post.

What have you done as far as providing the links to your IDE? (see this link: c++ - Linking dll in Visual Studio - Stack Overflow)

Do you have the SDL2.dll file? It should not be a .lib file. As mentioned by Yataro above, you can find the official library builds at libSDL’s github account: Release 2.28.4 · libsdl-org/SDL · GitHub and if you are still having trouble, then follow this tutorial by lazyfoo Lazy Foo' Productions - Setting up SDL 2 on Visual Studio 2019 Community Edition)