user2891869
user2891869

Reputation: 609

Undefined reference to 'SDL_main' while using Dev C++

i am currently having problem compiling my project in dev c++ which uses SDL libraries, what i did was first download the file SDL2-devel-2.0.3-mingw.tar.gz (MinGW 32/64-bit) from this site, then in

Tools -> Compiler Options and then in 'Directories' section i included the x86_64-w64-mingw32/lib in 'libraries' tab and x86_64-w64-mingw32/include in 'C++ includes' tab after i extracted the downloaded folder in C drive.

Finally, in project options i added these linkers

-lmingw32 -lSDL2main -lSDL2

But after compiling this code :

#include<stdio.h>
#include<SDL2/SDL.h>
int main(int argc, const char* argv[]) {
    printf("hi\n");
    return 0;
}

EDIT :

I tried removing const before char* and it said sdl.dll is missing so i downloaded the said file from the internet and pasted it where the project was and so the program was finally compiling but i am not getting any output as it should print "Hi"

I got the error that in Function console_main and undefined reference to sdl_main, can anyone help me rectify this problem.

Upvotes: 2

Views: 1507

Answers (1)

blakelead
blakelead

Reputation: 1890

This error is common when using int main() instead of :

int main(int argc, char **argv) 
//or
int main(int argc, char *argv[])

Try replacing it with either of these.

In the background, SDL defines a macro #define main SDL_main that renames your main(int argc, char *argv[]) function so that it does not conflict with its own main() function (used for SDL initialization). If you use main() instead, the macro does not modify it and SDL_main is then not found.

If it does not work, follow these steps:

  • When you create your project, make sure you choose a Win32 GUI or Win32 Console application type.

  • After creating your project, I assume you added the following command line to your project parameters under linker : -lmingw32 - -lSD2main -lSDL2

  • Then put SDL2.dll in your project directory where your executable will be.

  • Include SDL2.h before main(int argc, char **argv) begins in your source code.

Upvotes: 2

Related Questions