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

荒凉一梦 提交于 2019-12-20 02:52:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/33082126/undefined-reference-to-sdl-main-while-using-dev-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!