Compiling DLLs that use SDL to then use in games?

This post is probably going to come off as REALLY stupid to C++ & SDL experts, but please bear with me as I’m trying my best here and I’m eager to learn.

Okay, so I’ve worked in C++ intermittently over the last few years, however most of my experience is in C# in the commercial engine of Unity. I’m turning away from prefabricated engines in general and am creatively more interested in C++ and SDL for my future and this post is part of my effort to branch out. Back in October, I spent about a month daily learning how to use SDL, and it went really well, but I got stumped when it came to how I wanted to structure my project. My IDE is VS2019 and so I’ll be using terminology unique to that IDE in this explanation:

I want to make a solution that calls upon and uses SDL directly and compiles to a DLL or set of DLLs should the solution be made up of multiple projects. The projects that represent these DLLs will be maintained and updated in isolation of any particular game project. With the compiled DLLs from these projects, I will link them to game projects and have 99% of SDL functionality be syphoned through my DLLs such that my DLLs act as an engine for my games.

People have tried to explain this to me before with things like CMake as to how I would compile my .cpp and .h files into .o files and then into .dll files such that I can compile DLLs that make use of other DLLs such as the ones SDL provides, but I honestly just do not understand and I don’t think I ever will. Is there any way (with as much respect as possible) I can obfuscate the compile process of the DLLs I want to make through my IDE so I can focus on writing the code? I’m used to my IDE letting me click a button and spit out an executable, I want that but for a C++ DLL that makes calls to the SDL DLLs.

Many thanks for reading!

Creating the DLL:
Follow this walkthrough for creating a Dynamically Linked Library on Windows VS2019, the dll can link to SDL just like any other program.

Loading the DLL:
If you intend on linking SDL to the main program as well (or perhaps into a dll-manager class): SDL2 has an API for loading dll’s from an absolute path:
void * fileHandle = SDL_LoadObject(“AbsolutPathToDLL”);
function_pointer = SDL_LoadFunction(fileHandle, “function_Name”);

Use SDL_UnloadObject(fileHandle) when you are done with the dll library. (This will also destroy the function, so it’s best to set the function pointer to NULL and don’t use the function pointer until it is reloaded as another function.)

If you are using C++ you can pass an entire object from a class (created as a pointer on the heap) using a “factory function”, just make sure that there’s a correlating destruction function in the same DLL to ensure the object is destroyed/deleted in the same library unit (and call destroy on the object before the dll is unloaded).

It is surprisingly straight foreword as long as you are comfortable in using pointers to functions.

Where the power of this comes in is that you can set up your program to load that absolute path as a string from just about anywhere at run-time, be it hard-coded in the program, text from a drag and drop event, or a list from a file in your project. The dll is essentially a hot-plug plugin at that point. If done properly it can be added, utilized, then removed all while the program continues running.