11

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 ===|
vallentin
  • 23,478
  • 6
  • 59
  • 81
Mihai
  • 307
  • 2
  • 6
  • 14

8 Answers8

42

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

k1988
  • 521
  • 1
  • 4
  • 3
12

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

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
adderly
  • 192
  • 1
  • 7
  • 2
    I see the tutorials you showed me are about sdl 1.2 . I was talking about sdl 2.0 , as shown in the title.Now as far as I know, I tried before these linkers and they didn't work. I belive the new ones are something like: -lmingw32 -lSDL2main -lSDL2 . But even like that something is missing... – Mihai Jun 12 '13 at 18:04
  • http://wiki.libsdl.org/moin.fcg/FAQWindows :D your solution is pointed there apparently. – adderly Jun 12 '13 at 20:21
  • I don't understand something. "When you're compiling with gcc, you need to make sure the output of sdl-config follows your source file on the command line: gcc -o test test.c sdl-config --cflags --libs" How do you do that? – Mihai Jun 14 '13 at 14:45
  • ---I'VE HOUND THE SOLUTION!-- – Mihai Jun 14 '13 at 18:28
  • 2
    On codeblocks 10.05: "Settings"->"Compiler and Debugger"->"Linker Settings". On "Linker Libraries" press the "Add" button and add separatly "mingw32","SDL2main","SDL2","SDL2_image" in this order without "".and on the"Other Linker Options", add "-mwindows " without "".Also make sure you linked the right folders on the "Search Directories".On the "Compile" Pannel, "add" the link to the "include" folder from your PC, and on "Linker" Pannel, "Add" the link to the "lib", after you've added the new files(from sdl_img and mixer folder) to the original SDL folder – Mihai Jun 14 '13 at 18:35
9

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

mingw32
SDL2main
SDL2

Note that the order is important.

barribow
  • 109
  • 2
  • 4
5

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

Yash Gupta
  • 578
  • 6
  • 15
2

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

Leroy Dunn
  • 21
  • 1
2

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.

Striezel
  • 3,693
  • 7
  • 23
  • 37
Eyfenna
  • 29
  • 2
1

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"

Softmixt
  • 1,658
  • 20
  • 20
  • `target_link_libraries(space_fighters PUBLIC mingw32 SDL2::SDL2main SDL2::SDL2)` for Modern CMake. Not using `PUBLIC`/`PRIVATE`/`INTERFACE` recursively downgrades targets to pre-CMake 2.8.12 behavior. – Xeverous Mar 03 '21 at 19:42
-2

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 .