Unable to bind SDL2 functions using MinGW

I'm relatively new to programming and I decided to try SDL, but I'm a bit stuck. I could not build the project in code blocks, and I get the "undefined" link to all SDL functions. I saw a lot of similar questions here, but none of the solutions seem to help. I already added the \ include \ SDL2 and \ lib folders to search for directories, I added SDL2Main and SDL2 to link libraries in the linker options, I even added -mwindows to the other linker options. In addition, I tried to establish a connection with the 64-bit version, but it got worse.

Here is my source code, pretty much copied right from the tutorial that I started:

#include <SDL.h> SDL_Window* g_pWindow = 0; SDL_Renderer* g_pRenderer = 0; int main(int argc, char* args[]) { // initialize SDL if(SDL_Init(SDL_INIT_EVERYTHING) >= 0) { // if succeeded create our window g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); // if the window creation succeeded create our renderer if(g_pWindow != 0) { g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0); } } else { return 1; // sdl could not initialize } // everything succeeded lets draw the window // set to black // This function expects Red, Green, Blue and // Alpha as color values SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255); // clear the window to black SDL_RenderClear(g_pRenderer); // show the window SDL_RenderPresent(g_pRenderer); // set a delay before quitting SDL_Delay(5000); // clean up SDL SDL_Quit(); return 0; } 

And here is the build log:

 mingw32-g++.exe -LC:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib -o bin\Debug\GeometryProject.exe obj\Debug\main.o -mwindows C:\MinGW\lib\libmingw32.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2main.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2.a obj\Debug\main.o: In function `SDL_main': C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:7: undefined reference to `SDL_Init' C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:13: undefined reference to `SDL_CreateWindow' C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:17: undefined reference to `SDL_CreateRenderer' C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:27: undefined reference to `SDL_SetRenderDrawColor' C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:29: undefined reference to `SDL_RenderClear' C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:31: undefined reference to `SDL_RenderPresent' C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:33: undefined reference to `SDL_Delay' C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:35: undefined reference to `SDL_Quit' C:\MinGW\lib\libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to ` WinMain@16 ' collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minutes, 0 seconds) 9 errors, 0 warnings (0 minutes, 0 seconds) 

Is there anything else I could try? I really would like to get this job and would appreciate any help.

+12
c ++ mingw codeblocks sdl
source share
2 answers

A possible problem is that your link to the SDL2 library is for a different architecture.

You have to use

 SDL2-2.0.1\i686-w64-mingw32 

instead

 SDL2-2.0.1\x86_64-w64-mingw32 

Also, after setting the library search path, use this notation to link to libraries

 -lmingw32 -lSDL2main -lSDL2 

It is much easier to read.

+12
source share

Instead of adding the full path, try -LC:/PATH_TO_SDL -lSDL2main -lSDL2 (or just one SDL option); lib and .a already known to the linker.

(not sure if you need to use either / or \ )

Since you are using C ++, replace include with the following:

 extern "C" { #include "SDL.h" } 

This tells the compiler to treat SDL as C - rather than C ++ code.

See also: SDL2 will not bind correctly

+2
source share

All Articles