SDL2 - Errors and Installation Help

Windows 7, 64x
First off, I have basic knowledge in C++. I know how to write code and use header/source files and classes. I don’t really understand linking libraries and dlls and all of that entirely and am a little lost and need some help setting SDL2 up in Code Blocks.

Building and compiling works fine in a basic project. However when I try to use SDL, I keep getting errors.
I think I might be having some problems when it comes to setting-up/linking SDL 2.0.0 in Code Blocks (w/MinGW) considering the only tutorials I can find are all for 1.2 versions. I’ve been trying to find solutions online for many hours and have found similar situations but trying to use the proposed solution never seems to work.

As far as my attempts go, I’ve been messing around with settings, files, and the code and I get different errors.

These are samples from the project:

Header File:

#ifndef CAPP_H
#define CAPP_H

#include “include/SDL/SDL.h”

class CApp
{
private:
bool Running;
SDL_Window* Surf_Display;

public:
    CApp();

    int OnExecute();

public:
    bool OnInit();
    void OnEvent(SDL_Event* Event);
    void OnLoop();
    void OnRender();
    void OnCleanup();

};

#endif

Source File:

#include “CApp.h”

CApp::CApp()
{
Surf_Display = NULL;
Running = true;
}

int CApp::OnExecute()
{
if(OnInit() == false)
{
return -1;
}

SDL_Event Event;

while(Running)
{
    while(SDL_PollEvent(&Event))
    {
        OnEvent(&Event);
    }

    OnLoop();
    OnRender();
}

OnCleanup();

return 0;

}

int main(int argc, char* argv[])
{
CApp theApp;
return theApp.OnExecute();
}

Settings > Compiler… > Global compiler settings - Linker settings
Nothing in link libraries.
Other linker options: -lmingw32 -lSDLmain -lSDL

When trying to change the directory in #include “SDL.h”, it sometimes says it can’t find the directory.
So I change it to “SDL/SDL.h” and if I get Error Set #2, I change -lSDLmain and -lSDL under “Other linker options” to -lSDL2main and lSDL2.dll. Then Error Set #2 doesn’t appear but Error Set #1 does.

Error Set #1:
Various undefinded references to SDL functions.
Undefinded reference to WinMain at 16.

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

mingw32-g++.exe -o bin\Debug\SDLTesting.exe obj\Debug\CApp.o obj\Debug\CApp_OnCleanup.o obj\Debug\CApp_OnEvent.o obj\Debug\CApp_OnInit.o obj\Debug\CApp_OnLoop.o obj\Debug\CApp_OnRender.o -mwindows
obj\Debug\CApp.o: In function ZN4CApp9OnExecuteEv': C:/Users/User/Documents/Coding/SDLTesting/CApp.cpp:20: undefined reference toSDL_PollEvent’
obj\Debug\CApp_OnCleanup.o: In function ZN4CApp9OnCleanupEv': C:/Users/User/Documents/Coding/SDLTesting/CApp_OnCleanup.cpp:5: undefined reference toSDL_Quit’
obj\Debug\CApp_OnInit.o: In function ZN4CApp6OnInitEv': C:/Users/User/Documents/Coding/SDLTesting/CApp_OnInit.cpp:5: undefined reference toSDL_Init’
C:/Users/User/Documents/Coding/SDLTesting/CApp_OnInit.cpp:10: undefined reference to SDL_CreateWindow' c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.1/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference toWinMain at 16’
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 0 warnings (0 minutes, 0 seconds)

Error Set #2:
ld.exe cannot find -lSDLmain
ld.exe cannot find -lSDL

Hi Kaifory,

It appears that you haven’t completely added the SDL2 folders to
Code::Blocks so it’s not finding all of your files.

There are a few routes you can take, depending on whether or not you plan
on building a lot of SDL2 applications (including other people’s projects).

#1 - Copy SDL2’s headers and library files to Code::Blocks/MinGWs path

  1. Copy the contents of c:/path/to/SDL2/include to c:/Program
    Files/CodeBlocks/MinGW/include/SDL (or include/SDL2 if you also have SDL
    1.2)
  2. Copy c:/path/to/SDL2/build/libSDL* files (may be .a or .lib)
    to c:/Program Files/CodeBlocks/MinGW/lib

#2 - Set the Code::Blocks Include and Library Paths

  1. In Code::Blocks, navigate the menus to Settings -> Compiler &
    Debugger Settings -> Search Directories (should be a dialog)
  2. Add an include path to c:/path/to/SDL2/include
  3. Add a library path to c:/path/to/SDL2/build/

Some of these paths are going to be wrong, since I’m currently on Linux
with no Code::Blocks install, so you may need to poke around the
CodeBlocks/MinGW folders to find the include and lib directories.
Also, if you plan on updating SDL2 regularly, I recommend approach #2,
since things should be just be either a git pull or folder merge to update.
If you copy things, you may have less issues with using other people’s
Code::Blocks projects with SDL2, BUT, updating will be a pain, and there is
no guarantee that they will also be accessing the headers through the same
path (ie #include “SDL2/SDL.h”).

Also be sure to copy the SDL dll files into your executable’s directory,
otherwise, your application won’t be able to load SDL properly and you’ll
get some nasty dialog errors.

I hope that helps,
-Alex

Thanks for the reply, and I followed your instructions for #1 but I’m still having the ‘undefined references’ to SDL functions and WinMain at 16.

Edit: I also tried #2 but the errors I get are ‘permission denied’.

c:/program files (x86)/codeblocks/mingw/bin/…/lib/gcc/mingw32/4.7.1/…/…/…/…/mingw32/bin/ld.exe: cannot find C:\Users\User\Documents\SDL\include: Permission denied
c:/program files (x86)/codeblocks/mingw/bin/…/lib/gcc/mingw32/4.7.1/…/…/…/…/mingw32/bin/ld.exe: cannot find C:\Users\User\Documents\SDL\lib: Permission denied

Kaifory wrote:

Thanks for the reply, and I followed your instructions for #1 but I’m still having the ‘undefined references’ to SDL functions and WinMain at 16.

Edit: I also tried #2 but the errors I get are ‘permission denied’.

c:/program files (x86)/codeblocks/mingw/bin/…/lib/gcc/mingw32/4.7.1/…/…/…/…/mingw32/bin/ld.exe: cannot find C:\Users\User\Documents\SDL\include: Permission denied
c:/program files (x86)/codeblocks/mingw/bin/…/lib/gcc/mingw32/4.7.1/…/…/…/…/mingw32/bin/ld.exe: cannot find C:\Users\User\Documents\SDL\lib: Permission denied

It seems that the linker is not able to find the SDL Library.

There should be a place to specify path to libraries in project settings
in Code:Blocks.
Make sure you have the path to SDL2.lib in that place.

Compiling with SDL on windows, you need SDL2.lib
(a) SDL2.lib is what you need to specify to the linker’

(b) SDL2.dll is what you need to actually run your compiled program.
SDL2.dll should be in the directory where your compiled exe is.

Finally, when giving path to the linker you only need to specify it like
this:
-lSDL2 -lSDL2_Main

Note that extension .lib is omitted.

Most likely Code:Blocks will have project settings where you can specify
the library by just adding their names like this:
SDL2
SDL2_Main

so you won’t have to manually add these options to the command like.

Best of luck!On 9/4/2013 10:23 AM, Kaifory wrote:

Windows 7, 64x
First off, I have basic knowledge in C++. I know how to write code and
use header/source files and classes. I don’t really understand linking
libraries and dlls and all of that entirely and am a little lost and
need some help setting SDL2 up in Code Blocks.

Building and compiling works fine in a basic project. However when I
try to use SDL, I keep getting errors.
I think I might be having some problems when it comes to
setting-up/linking SDL 2.0.0 in Code Blocks (w/MinGW) considering the
only tutorials I can find are all for 1.2 versions. I’ve been trying
to find solutions online for many hours and have found similar
situations but trying to use the proposed solution never seems to work.

As far as my attempts go, I’ve been messing around with settings,
files, and the code and I get different errors.

These are samples from the project:

Header File:

#ifndef CAPP_H
#define CAPP_H

#include “include/SDL/SDL.h”

class CApp
{
private:
bool Running;
SDL_Window* Surf_Display;

public:
CApp();

int OnExecute();

public:
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
};

#endif

Source File:

#include “CApp.h”

CApp::CApp()
{
Surf_Display = NULL;
Running = true;
}

int CApp::OnExecute()
{
if(OnInit() == false)
{
return -1;
}

SDL_Event Event;

while(Running)
{
while(SDL_PollEvent(&Event))
{
OnEvent(&Event);
}

OnLoop();
OnRender();
}

OnCleanup();

return 0;
}

int main(int argc, char* argv[])
{
CApp theApp;
return theApp.OnExecute();
}

Settings > Compiler… > Global compiler settings - Linker settings
Nothing in link libraries.
Other linker options: -lmingw32 -lSDLmain -lSDL

When trying to change the directory in #include “SDL.h”, it sometimes
says it can’t find the directory.
So I change it to “SDL/SDL.h” and if I get Error Set #2, I change
-lSDLmain and -lSDL under “Other linker options” to -lSDL2main and
lSDL2.dll. Then Error Set #2 doesn’t appear but Error Set #1 does.

Error Set #1:
Various undefinded references to SDL functions.
Undefinded reference to WinMain at 16.

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

mingw32-g++.exe -o bin\Debug\SDLTesting.exe obj\Debug\CApp.o
obj\Debug\CApp_OnCleanup.o obj\Debug\CApp_OnEvent.o
obj\Debug\CApp_OnInit.o obj\Debug\CApp_OnLoop.o
obj\Debug\CApp_OnRender.o -mwindows
obj\Debug\CApp.o: In function ZN4CApp9OnExecuteEv': C:/Users/User/Documents/Coding/SDLTesting/CApp.cpp:20: undefined reference toSDL_PollEvent’
obj\Debug\CApp_OnCleanup.o: In function ZN4CApp9OnCleanupEv': C:/Users/User/Documents/Coding/SDLTesting/CApp_OnCleanup.cpp:5: undefined reference toSDL_Quit’
obj\Debug\CApp_OnInit.o: In function ZN4CApp6OnInitEv': C:/Users/User/Documents/Coding/SDLTesting/CApp_OnInit.cpp:5: undefined reference toSDL_Init’
C:/Users/User/Documents/Coding/SDLTesting/CApp_OnInit.cpp:10:
undefined reference to SDL_CreateWindow' c:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.7.1/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference toWinMain at 16’
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 0 warnings (0 minutes, 0 seconds)

Error Set #2:
ld.exe cannot find -lSDLmain
ld.exe cannot find -lSDL


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Pallav Nawani
Game Designer/CEO
http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming

I moved what was in Settings > Compiler and all that to the Project Build Options and that seems to do the trick. I don’t understand why I’m getting different errors now though. SDL2.dll is where the project exe is but it is now saying the file and it’s format cannot be recognized.

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

mingw32-g++.exe -LC:\Users\User\Documents\SDL\bin -o bin\Debug\SDLTesting.exe obj\Debug\CApp.o obj\Debug\CApp_OnCleanup.o obj\Debug\CApp_OnEvent.o obj\Debug\CApp_OnInit.o obj\Debug\CApp_OnLoop.o obj\Debug\CApp_OnRender.o -lmingw32 -lSDL2 -lSDL2main -mwindows
C:\Users\User\Documents\SDL\bin/SDL2.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds)

Pallav Nawani wrote:

It seems that the linker is not able to find the SDL Library.

There should be a place to specify path to libraries in project settings
in Code:Blocks.
Make sure you have the path to SDL2.lib in that place.

Compiling with SDL on windows, you need SDL2.lib
(a) SDL2.lib is what you need to specify to the linker’

(b) SDL2.dll is what you need to actually run your compiled program.
SDL2.dll should be in the directory where your compiled exe is.

Finally, when giving path to the linker you only need to specify it like
this:
-lSDL2 -lSDL2_Main

Note that extension .lib is omitted.

Most likely Code:Blocks will have project settings where you can specify
the library by just adding their names like this:
SDL2
SDL2_Main

so you won’t have to manually add these options to the command like.

Best of luck!

If this information is helpful at all: I am also downloading and using files from only: SDL2-devel-2.0.0-mingw.tar.gz (I’m using the 64x stuff from the decompressed files) and there doesn’t seem to be a build folder. Only bin, include, lib, and share folders along with a MakeFile. There also aren’t any .lib files but there are some .a files and a .la file in the lib folder.

If that is easy for you, and you’ll be using SDL2, just for compiling your programs, try this method:

-> Remove anything that has to do with SDL from Codeblocks/MinGW.

-> Download the mingw binaries from the SDL website

-> From the appropriate folder (x86, x64 or whatever they are called), do:

  • copy the contents of ‘include’ at ‘mingw/include/SDL2/’

  • copy the contents of ‘lib’ at ‘mingw/lib’

  • copy the contents of ‘bin’ at ‘mingw/bin’*

Then, you will be able to compile all SDL2 projects by doing the following:

#include SDL2 files like this:

#include “SDL2/SDL.h”

And add the following at the extra linker settings (note the order):

-lSDL2_main -lSDL2

Hope it helps,
Aggelos Kolaitis

  • Doing this means that you wont have to copy SDL2.dll at the same directory where the executable is. This could show errors, however, if you try to compile other people’s code. I mainly work under Linux, so that’s my setup which I use to test my code under Windows. Well, it works for me :)On Sep 5, 2013, at 9:41 AM, “Kaifory” <glissando.sc at gmail.com> wrote:

Pallav Nawani wrote:
It seems that the linker is not able to find the SDL Library.

There should be a place to specify path to libraries in project settings
in Code:Blocks.
Make sure you have the path to SDL2.lib in that place.

Compiling with SDL on windows, you need SDL2.lib
(a) SDL2.lib is what you need to specify to the linker’

(b) SDL2.dll is what you need to actually run your compiled program.
SDL2.dll should be in the directory where your compiled exe is.

Finally, when giving path to the linker you only need to specify it like
this:
-lSDL2 -lSDL2_Main

Note that extension .lib is omitted.

Most likely Code:Blocks will have project settings where you can specify
the library by just adding their names like this:
SDL2
SDL2_Main

so you won’t have to manually add these options to the command like.

Best of luck!

If this information is helpful at all: I am also downloading and using files from only: SDL2-devel-2.0.0-mingw.tar.gz (I’m using the 64x stuff from the decompressed files) and there doesn’t seem to be a build folder. Only bin, include, lib, and share folders along with a MakeFile. There also aren’t any .lib files but there are some .a files and a .la file in the lib folder.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Finally it is working! Thank you all for your help. :slight_smile: