Search code examples
c++sdl

Can't compile simple SDL program with Dev-C++


Just before you read just know that i'm not english, so hopefully i won't misspell my writing here. Anyway. I was trying to compile my first SDL program, so i followed online tutorials to install SDL2 libraries. The code (copied from here min 13:00) i used is this :

#include <iostream>
#include <SDL2/SDL.h>
using namespace std;

int main(void){

    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        cout << "SDL init failed.\n";
        return 1;
    }

    cout << "SDL init succeeded";
    SDL_Quit();


    return 0;
} 

The error i get is this

C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\SDL2-2.0.12\x86_64-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o) In function `main_getcmdline':
71      s:\rs\valve\release\SDL\SDL2-2.0.12-source\src\main\windows\SDL_windows_main.c  undefined reference to `SDL_main'
C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\Programs\SDL_\collect2.exe   [Error] ld returned 1 exit status
25      C:\Users\raffaele.ciotola\Desktop\Marco & Lory\Lorenzo\Dev-Cpp\Programs\SDL_\Makefile.win   recipe for target 'SDL_.exe' failed

I tried running my Dev-Cpp.exe on administrator, since the installation folder is on the desktop, but that didn't solve the problem.

The Makefile (Whatevere it is, i don't have the minimal idea) is this. If needed ¯_(ツ)_/¯.

# Project: Progetto3
# Makefile created by Dev-C++ 5.11

CPP      = g++.exe
CC       = gcc.exe
WINDRES  = windres.exe
OBJ      = SDL_.o
LINKOBJ  = SDL_.o
LIBS     = -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib" -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -L"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/SDL2-2.0.12/x86_64-w64-mingw32/lib" -static-libgcc -mwindows -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lglu32
INCS     = -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS  = -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++" -I"C:/Users/raffaele.ciotola/Desktop/Marco & Lory/Lorenzo/Dev-Cpp/SDL2-2.0.12/x86_64-w64-mingw32/include"
BIN      = SDL_.exe
CXXFLAGS = $(CXXINCS) 
CFLAGS   = $(INCS) 
RM       = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
    ${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
    $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)

SDL_.o: SDL_.cpp
    $(CPP) -c SDL_.cpp -o SDL_.o $(CXXFLAGS)

If you need any other info just ask. Thank You.


Solution

  • SDL hijacks the main function with its own in order to do some initial setup. It then calls whatever you have written as the main function. Because it is calling your main function it expects it to be defined in a specific way.

    Try this and it should resolve the errors you are encountering:

    int main(int argc, char* args[])
    {
        // whatever
        return 0;
    }