Mihai
Mihai

Reputation: 343

SDL 2 Undefined Reference to "WinMain@16" and several SDL functions

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 ===|

Upvotes: 13

Views: 44423

Answers (8)

Softmixt
Softmixt

Reputation: 1756

If you are using CMakeLists.txt which is default in cLion IDE you should have this line at the end:

target_link_libraries(space_fighters mingw32 SDL2main SDL2)

I had same issue and my case was because i missed "mingw32"

Upvotes: 2

Charles Kirk
Charles Kirk

Reputation: 9

I just had a similar issue when trying to build and run a project in Eclipse CDT 1909 on Windows 10 that I had originally created in Eclipse on MacOS.

I found the following steps worked using the minGW64 Compiler.

RMB Click on the project and choose properties. In Project Properties > C/C++ Build create a new C/C++ Build Configuration by LMB Clicking Manage Configurations and LMB Clicking New, name it Debug_Windows, set Copy Setting from to Default configuration, make it active, LMB Click OK, set the Configuration to Debug_Windows.

Set the Project Properties > C/C++ Build > Builder Settings > Build Command > Current Builder to CDT Internal Builder

Set the Project Properties > C/C++ Build > Builder Settings > Tool Chain Editor > Current toolchain to MinGW GCC

Set the Project Properties > C/C++ Build > Builder Settings > Tool Chain Editor > Current builder to CDT Internal Builder

Add the following path to the Project Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes "C:\Applications\Library\SDL2-2.0.9\x86_64-w64-mingw32\include\SDL2"

Add the following path to Project Properties > C/C++ Build > Settings > GCC C++ Linker > Libraries "C:\Applications\Library\SDL2-2.0.9\x86_64-w64-mingw32\lib"

add the following libraries to Project Properties > C/C++ Build > Settings > GCC C++ Linker > Libraries, in the order SLD2main SDL

Navigate to Project Properties > Run/Debug Settings, LMB Click New, select C/C++ Application, LMB Click OK, in the C/C++ Application field enter Debug_Windows\project_name.exe (replacing project_name with the correct value).

Change the Build Configuration to Debug_Windows.

Close the project properties.

Add the preprocessor directive #define SDL_MAIN_HANDLED at the top of the file containing the main function. Without the preprocessor directive the program will not link correctly.

To run the program RMB Click on the project and choose Run as > Run Configurations and choose the run configuration created for the build.

Copy SDL2.dll to the top level of the project directory structure, as if it is copied to the Debug or Debug_Windows directory it is deleted every time the project is built.

The key points that I found were that it is not necessary to include mingw32 in the Linker Libraries and #define SDL_MAIN_HANDLED needs to be in the file containing main and before #include .

Upvotes: -2

adderly
adderly

Reputation: 192

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

Upvotes: 12

barribow
barribow

Reputation: 111

You need to add these to linker libraries with the same order:

mingw32
SDL2main
SDL2

Note that the order is important.

Upvotes: 10

k1988
k1988

Reputation: 591

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

Upvotes: 49

Eyfenna
Eyfenna

Reputation: 29

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.

Upvotes: 2

Yash Gupta
Yash Gupta

Reputation: 588

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

Upvotes: 5

Leroy Dunn
Leroy Dunn

Reputation: 21

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

Upvotes: 2

Related Questions