1

Basically when using SDL2 library why do I need to use both SDL2.dll (dynamic) file and .a (static) for compilation?

For an example to compile a simple program I need to tell gcc where lib files (static .a) are located to compile the program. Why not just use the .dll file instead?

My first thought is that .a files are needed for the compiler to check if program can compile with the library and .dll is only needed when running the program (and program is not statically linked in the end), but still that wouldn't explain why do i need the .a files instead of just .dll file.

example:

gcc -I src/include src/lib -o main.exe main.c -lmingw32 -lSDL2main -lSDL2
genpfault
  • 51,148
  • 11
  • 85
  • 139
Goh
  • 33
  • 3
  • When you call a function the compiler needs to know where to look to find it. The library you statically link just contains what DLL and export name the program needs to look for. Otherwise, how would it know what DLL to load? That's why the statically linked libraries are so small - they don't contain any program code. – Jerry Jeremiah Jan 17 '23 at 20:15
  • Dont .h files in include file suffice for that tho? They have function declarations. Also .a files weight 25MB and .dll file weights 2MB. Also I noticed there is "libSDL2.a" file in lib folder that pretty much weights 22MB – Goh Jan 17 '23 at 20:26
  • @Goh you don't need both. Why do you think you do? It may have been the case if you're talking about libSDL2.dll.a (import library, not actual static library), but it seems that you're not? – keltar Jan 18 '23 at 05:33
  • @JerryJeremiah that's only correct for import libraries, which are kind of microsoft-specific (at least I haven't seen any other OS that uses that concept). Import library is basically an indirection table, which could be generated by compiler/linker automatically, and it is done automatically on non-MS OSes. Mingw could do that even on windows if you link with dll and don't have `dll.a`. Actual static libraries (not import libraries) do contain code and don't require any dll at runtime. – keltar Jan 18 '23 at 05:43
  • Yes, I am a Microsoft specific person. I didn't realize that it was a Microsoft specific thing. I just assumed that if you had libs and DLLs they would be related. Maybe they just want to give you the ability to statically link or dynamically link? But you wouldn't do both at the same time... – Jerry Jeremiah Jan 18 '23 at 20:25

1 Answers1

2

There are two .a files: libSDL2.a and libSDL2.dll.a (not counting libSDL2main.a, which is always static).

The first one is a true static library. -lSDL2 doesn't prefer it by default, it prefers libSDL2.dll.a. And if you force it to use the former, because the latter is unavailable, the resulting app won't depend on SDL2.dll.

The second one is an import library. In the old days, MinGW couldn't link against a .dll directly, and you had to use those. Modern MinGW can link .dlls directly, and those should in theory be unnecessary.

I'd still recommend using the import library if it's available, just because it's more common and more widely tested.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207