VCake VCake - 8 months ago 76
C++ Question

SDL2 Undefined references to functions

I'm trying to set up a C++ project that builds on MinGW and uses SDL. When i try to compile my program, g++ says

undefined reference to 'SDL_Function'
for each SDL function i use.

Lib/SDL2: contents of SDL2-devel-2.0.0-mingw.tar.gz from SDL's website


#include "SDL.h"

int main(int argc, char *argv[]) {

SDL_Window *window = SDL_CreateWindow(
"Hello World",
640, 480, 0

return 0;

I'm using Rake to simplify the build process a little, the commmand it produces is:

g++ -Wall -ILib/SDL2/i686-w64-mingw32/include/SDL2 -Dmain=SDL_main -LLib/SDL2/i686-w64-mingw32/lib -lmingw32 -lSDL2main -lSDL2 -mwindows Source/Main.cpp -o Build/sdltest.exe

And here's what g++ says:

Main.cpp:(.text+0xe): undefined reference to `SDL_Init'`
Main.cpp:(.text+0x42): undefined reference to `SDL_CreateWindow'`
Main.cpp:(.text+0x51): undefined reference to `SDL_Delay'`
Main.cpp:(.text+0x5c): undefined reference to `SDL_DestroyWindow'`
Main.cpp:(.text+0x61): undefined reference to `SDL_Quit'`
ld.exe: bad reloc address 0x20 in section `.eh_frame'`
ld.exe: final link failed: Invalid operation`
collect2.exe: error: ld returned 1 exit status`

It looks like a common beginner's problem but i think i pass all the troubleshooting points:

  • Main function has
    int argc, char *argv[]

  • I use all the flags from sdl-config (-Dmain=SDL_main -lmingw32 -lSDL2main -lSDL2 -mwindows)

  • I'm trying to use the 'i686-w64-mingw32' version as supposed when using 32-bit MinGW

  • All specified paths seem correct

Any clue what's going on?

Answer Source

Try to change the sequence of the input params:

I've stumbled over this before (on Linux):

This call produces an error:

g++ $(pkg-config --cflags --libs sdl2) sdl2test.cpp 

sdl2test.cpp:(.text+0x11): undefined reference to `SDL_Init'
sdl2test.cpp:(.text+0x20): undefined reference to `SDL_GetError'
sdl2test.cpp:(.text+0x34): undefined reference to `SDL_Quit'

This works:

g++ sdl2test.cpp $(pkg-config --cflags --libs sdl2)