Goh
Goh

Reputation: 33

Why does SDL2 need both static and dynamic libraries for compilation

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

Upvotes: 1

Views: 1712

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96326

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.

Upvotes: 3

Related Questions