undefined reference to SDL_Init

miquel09 picture miquel09 · May 10, 2015 · Viewed 24.5k times · Source

I started using SDL today and had some trouble before, now I got it running but it won't let me init it.

This is my code:

#include <iostream>
#include "SDL.h"
#undef main

using namespace std;

int main(){
    if(SDL_Init(SDL_INIT_EVERYTHING)<0){
        cout << "error starting sdl" << endl;
    }
    return 0;
}

This the build log:

-------------- Build: Debug in Graphics (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -g -std=c++11 -IC:\Users\73638G75MA\Documents\SDL2-2.0.3\x86_64-w64-mingw32\include\SDL2 -I"C:\Users\73638G75MA\Documents\C++ projects\Graphics" -c "C:\Users\73638G75MA\Documents\C++ projects\Graphics\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe -LC:\Users\73638G75MA\Documents\SDL2-2.0.3\x86_64-w64-mingw32\lib -o bin\Debug\Graphics.exe obj\Debug\main.o  -lmingw32 -lSDL2main -lSDL2  
obj\Debug\main.o: In function `main':
C:/Users/73638G75MA/Documents/C++ projects/Graphics/main.cpp:8: undefined reference to `SDL_Init'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

I would apreciate all possible help in this, the #undef main at the start is because it won't let me run it otherwise. If it isn't there it gives me a "undefined reference to winmain@16" While I am creating a console application.

Answer

David Macek picture David Macek · May 11, 2015

According to the include and library search paths ...\SDL2-2.0.3\x86_64-w64-mingw32\..., you're trying to build with a 64-bit SDL2. Judging from the compiler name mingw32-g++, I'd say you're using the mingw.org toolchain According to the Code::Blocks download page and my inspection of the contents of codeblocks-13.12mingw-setup.exe, the included toolchain is 32-bit only and can't create 64-bit binaries nor use 64-bit libraries.

If you want to use a pre-built SDL2, you either need to download the matching toolchain (64-bit mingw-w64) and use that, or change your build parameters to use the 32-bit build of SDL2 (it's present in the development libraries archive in the i686-w64-mingw32 directory).