0

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
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • `-l:libSDL2.a` is usually spelled `-lSDL2`. Your libraries are in the wrong order, and you forgot `-lmingw32`. It should be `-lmingw32 -lSDL2main -lSDL2`. – HolyBlackCat Apr 04 '22 at 09:33
  • Your [answer to the other question](https://stackoverflow.com/questions/64396979/how-do-i-use-sdl2-in-my-programs-correctly) was very helpful and I am surprised that I hadn't found it when looking for answers initially. Turns out I was using a 32-bit compiler and the tip about printing the size of a void pointer helped a lot, thank you. – Ivan Gyulev Apr 04 '22 at 19:16

1 Answers1

0

Well this solution works assuming the dll is supposed to be where it needs to and windows/gcc knows where it is, heres a so answered question that solves your issue in case gcc doesn't find the sdl2 library(im more used to work with Linux which has a dedicated library path) also you will prob add -LC:\your_library_path to the second gcc command, as you pulled the dll out of the library directory

I've took your code and ran it with the following commands

gcc main2.c -c
gcc main2.o -o main2 -lSDL2

and here's your code

#include <stdlib.h>
#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;
}

and it "just worked", well that is until i executed the actual executable, it gave me a "SDL Error" and stopped the execution, but the above answers your question

Imeguras
  • 436
  • 6
  • 18