I am learning SDL for some month now, which dramatically increased my C++ skills.
But I have some questions about the use/general function of DLL files:
1. My interpretation of DLL file is a collection of functions and classes (not necessarily C/C++) provided for other programs. Is this right?
2. On different sources I read about "static linking" & "dynamic linking" but I am not completely sure about it's meanings:
If I use #include <SDL/SDL.h> the header file for SDL.dll is directly included into my project when compiled. I hope till this point I am not wrong...
This way the DLL is linked static because the specific header for this DLL version was included, isn't is?
So you can't exchange the DLL version because of eventually lacks of compatibility?
And if I want to link a DLL dynamic I include <windows.h>. I have a bunch of typedefs for the used functions like:
typedef int (*SDL_Init_i) (Uint32);
And then I have to get the address of the called function at runtime with some code like this:
SDL_Init_i iSDL_init;
HINSTANCE hSDL = LoadLibrary("SDL.dll");
if (hSDL)
{
iSDL_init = (SDL_Init_i) GetProcAddress(hSDL, "SDL_init");
if ( !iSDL_init)
{
return 1;
}
}
else
{
return 1;
}
After that I hopefully can use the function probably this way for example:
if (iSDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
{
log->log("SDL init error...");
return false;
}
At the end of my programm I have to free my program with FreeLibrary(hSDL);.
This way I have the possibility of changing the SDL.dll for example with a newer version, haven't I?
So now the actual questions for 2. ;):
- Is my knowledge about DLL linking correct?
- If it is: Isn't there a cross platform solution for dynamic linking or: how would this be done in a Linux environment?
- I have chosen the example above because it shows an other problem: If I link a DLL dynamic, how can I use special flags or classes out of this DLL (like SDL_INIT_VIDEO or SDL_Rect as class)?
3. If I am compiling I always use the following flags: -lSDL -lSDLmain. I know they are used for the Linker and they tell what to use but why are there two for one DLL? If I also use SDL_image.DLL I have to add -lSDL_image. So exactly: What is the -lSDLmain flag for?
3.a. Is there a difference in linking static/dynamic and the use of flags? Because I "hardcode" the DLL filepath when linking dynamic, so there is no need of extra compiler flags?
4. Why are there Runtime Libraries & Development Libraries? Just a missing documentation or what is the difference?
5. Inside the lib folder of SDL there are .a files. What are they used for? I read some tutorials about DLL's and the .a files are a product of creating DLL's (surprise...). Are they part of dynamic/static links or are they needed always?
EDIT:
6. If I link dynamic: Is it better to include all needed DLL-functions/classes/etc. at once at the beginning, or should I load the DLL every time a function is needed?
I am not sure because I don't know how fast DLL's are. Maybe they are a bottleneck, I don't know;). Or it would consume too much memory to save all function-addresses the whole Runtime?
I hopefully didn't scare you with my bad English skills and/or my newbie problems;).
I am thankful for every help.
Nazrim