问题
I've just installed SDL 2 and I have some serious problems. This is my code:
#include <SDL2\SDL.h>
int main(int argc, char* argv[]){
SDL_Init( SDL_INIT_VIDEO );
SDL_Quit();
return 0;
}
I am unable to compile because I get the error described in the title:
obj\Debug\main.o||In function SDL_main':|
C:\Users\myuser\Desktop\test 2000\main.cpp|5|undefined reference to SDL_Init'|
C:\Users\myuser\Desktop\test 2000\main.cpp|7|undefined reference to SDL_Quit'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to WinMain@16'|
||=== Build finished: 3 errors, 0 warnings ===|
回答1:
post the compiler commands. ex: g++/gcc ....
you are probably not linking the library.
http://content.gpwiki.org/index.php/SDL:Tutorials:Setup you should have the path to the lib, included in the ide. (I see you are using codeblocks)
add to the linker settings: -lmingw32 -lSDLmain -lSDL
http://www.sdltutorials.com/sdl-tutorial-basics
回答2:
I think you want
#define SDL_MAIN_HANDLED
in your main file, BEFORE the line
#include <SDL2/SDL.h>
from: https://stackoverflow.com/a/32343111/5214543
回答3:
Although the accepted answer works (if you follow it exactly, or read the comments), there's one caveat: You have to follow the order of linking libraries as given
g++ 01_hello_SDL.cpp -I{add correct path here}/include/SDL2 -L{add correct path here}/lib -lmingw32 -lSDL2main -lSDL2
And if you dont want a console to run alongside the window, add -mwindows
to the options
回答4:
You need to add these to linker libraries with the same order:
mingw32
SDL2main
SDL2
Note that the order is important.
回答5:
I struggled with this forever.
Simply move the sdl files out of your program files to your desktop or documents etc and link them to C from there.
Think it has something to do with Windows not allowing C to access them or something.
Hope this helps Cool
回答6:
I solved this by exchanging the
int main(int argc, char* argv[])
line with
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
No idea why to be honest, then again I moved on to the SFML.
来源:https://stackoverflow.com/questions/17048072/sdl-2-undefined-reference-to-winmain16-and-several-sdl-functions