2

I'm using Code::Blocks, that's my code:

#include "SDL2/SDL.h"
int main(int argc, char* args[]) {
    SDL_Init( SDL_INIT_EVERYTHING );
    SDL_Quit();
    return 0;
}

I'm building like:

mingw32-g++.exe -o C:\..\main.exe C:\..\main.o  -lmingw32 -lSDL2main -lSDL2

And getting that:

undefined reference to "SDL_Init"
undefined reference to "SDL_Quit"

I'm pretty sure the linker finds the libs cause if I change them to something random it complains "can't find whatever".

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • If you "change them to something random" it becomes the compiler that complains and you don't get as far as the linker. – 1ace Jun 30 '13 at 00:52
  • That makes a lot of sense, but that was what the documentation said to link to. If I try adding an absolute path to these libraries I get the exact same error, so I _suppose_ they are right. :( –  Jun 30 '13 at 01:00
  • Wrong link order? Try to swap SDL2main and SDL2, maybe it works. – Thomas Jun 30 '13 at 03:09

3 Answers3

6

A bit late, but I just stumbled over a similar problem on Linux.

This results in linker errors:

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)
zeroc8
  • 853
  • 1
  • 10
  • 20
6

even if this is an Linux Problem, i came throug this post via google.

Mayb it could help someone else: i had the same Problem with Windows XP 32bit Codeblocks: Mingw + SDL2 and i fixed it after i copied the right SDLFolders (include, lib...) to the mingw-folder. The reason why i trapped to this pifall is that the naming of the SDL-Rootfolder, out of the DEV-Pack is a bit confusing. You got a "x86_64-w64-mingw32"-Folder which is for 64bit Compiler and "i686-w64-mingw32" which is for 32bit Compiler (like the moste still are). i messed this up because of the "x86..."naming, still dont know why they are writing it this way.

After overwriting the right files it works fine for me.

Rocky51
  • 61
  • 1
  • 3
0

Are you sure you have your libraries in you path ?

Try adding -LC:/whatever/ with the folder that actually contains you libSDL2.a and other *.a to your compiler's arguments.

1ace
  • 5,272
  • 4
  • 23
  • 30
  • Note that you should add the `-L` argument before the corresponding `-l` to make sure the linker knows where to find each lib. – 1ace Jun 30 '13 at 01:01
  • So you mean like that: **-L"C:\Program Files (x86)\CodeBlocks\MinGW\sdl\lib"** before -o? Where should I put *.a ? –  Jun 30 '13 at 01:11
  • The argument is `-L`, not `-LC`, to which you append the path, and you should convert your `\` to `/`. As for the position, is should come anywhere before your first `-l` which asks for a lib that is in that folder, so yes, putting it as the first argument would work. In your case, this would give `mingw32-g++.exe -o C:\..\main.exe C:\..\main.o -lmingw32 -L"C:/Program Files (x86)/CodeBlocks/MinGW/sdl/lib" -lSDL2main -lSDL2`. – 1ace Jun 30 '13 at 01:19