Recently I stopped using IDEs in an attempt to learn more about the infrastructure of programming; I started compiling my C++ and C code with MinGW's compilers(g++.exe
and gcc.exe
accordingly) and the help of PowerShell.
This is all worked out well until I wanted to use SDL again for a project and failed miserably at manual linking. I want to preface this by saying that I've read a lot about linking, .dll
files, gcc
and compiler flags and I am struggling to understand the way the compiler works.
My basic understanding is that when you want to use a DLL you need a library file to link the names of the functions, variables, etc. from your header file with the location of those things in the binary .dll
file, but this seems to not be always the case, because, when I made my own DLL(gcc -shared -o mydll.dll mydll.o
) I only had to link it like so gcc main.c -o main -L . -l:mydll.dll
, no library file whatsoever.
I though that if I manage to compile SDL and maybe something I haven't tried like GLFW or even OpenGL I would understand it, but after a ton of reading and trying I still haven't succeeded to do so. And to start it small I only care about compiling a small program with SDL in C and I will only show one of my attempts to do so.
STEP 1
I got the development library for MinGW from here. Got the contents from the x86_64-w64-mingw32
folder as that is the OS I am using. Took out the SDL2.dll
from the bin folder in the main folder so it was where the executable would be.(I avoided folders for binaries, object files, source code and headers to keep things simple until I learned what I was doing). Then I removed the shared folder and the bin folder as I thought I didn't need them. I was left with the following folder contents(after making a main.c
file):
STEP 2
Wrote the following in the main.c
file:
#include <stdio.h>
#include <SDL2/SDL.h>
int main(int argc, char *argv[]) {
if (!SDL_Init(SDL_INIT_EVERYTHING)) printf("SDL Error: %s\n", SDL_GetError());
SDL_Window *window = SDL_CreateWindow("test", 0, 0, 960, 540, SDL_RENDERER_ACCELERATED);
SDL_Delay(5000);
SDL_Quit();
return 0;
}
STEP 3
Tried to compile with one command(skipping turning everything into an object file first):
gcc main.c -o main -L lib -l:libSDL2.a -l:libSDL2main.a -I include
STEP 4
Got this error message which is the furthest I've always gone with compiling SDL or GLFW
C:\Users\igyul\AppData\Local\Temp\cc4Vbdvm.o:main.c:(.text+0xe): undefined reference to `SDL_Init'
C:\Users\igyul\AppData\Local\Temp\cc4Vbdvm.o:main.c:(.text+0x17): undefined reference to `SDL_GetError'
C:\Users\igyul\AppData\Local\Temp\cc4Vbdvm.o:main.c:(.text+0x5b): undefined reference to `SDL_CreateWindow'
C:\Users\igyul\AppData\Local\Temp\cc4Vbdvm.o:main.c:(.text+0x6a): undefined reference to `SDL_Delay'
C:\Users\igyul\AppData\Local\Temp\cc4Vbdvm.o:main.c:(.text+0x6f): undefined reference to `SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status