Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL2 Undefined references to functions

Tags:

c++

mingw

sdl

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

Source/Main.cpp:

#include "SDL.h"

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

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

    SDL_Delay(3000);
    SDL_DestroyWindow(window);
    SDL_Quit();
    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?

like image 346
VCake Avatar asked Dec 05 '22 09:12

VCake


1 Answers

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)
like image 53
zeroc8 Avatar answered Dec 09 '22 14:12

zeroc8