# SDL2 - Errors and Installation Help

**URL:** <https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944>\
**Category:** SDL Development\
**Created:** [September 4, 2013, 4:53am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944 "2013-09-04T04:53:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaifory](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/dfb087/32.png) [@Kaifory](https://discourse.libsdl.org/u/Kaifory)\
**Post date:** [September 4, 2013, 4:53am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/1 "2013-09-04T04:53:53Z")

</div>

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 to`SDL\_PollEvent’  
obj\Debug\CApp\_OnCleanup.o: In function `ZN4CApp9OnCleanupEv': C:/Users/User/Documents/Coding/SDLTesting/CApp_OnCleanup.cpp:5: undefined reference to`SDL\_Quit’  
obj\Debug\CApp\_OnInit.o: In function `ZN4CApp6OnInitEv': C:/Users/User/Documents/Coding/SDLTesting/CApp_OnInit.cpp:5: undefined reference to`SDL\_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 to`WinMain 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

---

<div class="post-metadata">

**Author:** ![Alex\_Barry](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/alex_barry/32/515_2.png) [@Alex\_Barry](https://discourse.libsdl.org/u/Alex_Barry)\
**Post date:** [September 4, 2013, 5:34pm UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/2 "2013-09-04T17:34:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kaifory](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/dfb087/32.png) [@Kaifory](https://discourse.libsdl.org/u/Kaifory)\
**Post date:** [September 5, 2013, 1:41am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/3 "2013-09-05T01:41:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kaifory](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/dfb087/32.png) [@Kaifory](https://discourse.libsdl.org/u/Kaifory)\
**Post date:** [September 5, 2013, 1:53am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/4 "2013-09-05T01:53:17Z")

</div>

> [@Kaifory](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![Kaifory](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/dfb087/32.png) [@Kaifory](https://discourse.libsdl.org/u/Kaifory)\
**Post date:** [September 5, 2013, 1:53am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/5 "2013-09-05T01:53:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Pallav\_Nawani](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/pallav_nawani/32/237_2.png) [@Pallav\_Nawani](https://discourse.libsdl.org/u/Pallav_Nawani)\
**Post date:** [September 5, 2013, 5:58am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/6 "2013-09-05T05:58:30Z")

</div>

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 to `SDL\_PollEvent’  
> obj\Debug\CApp\_OnCleanup.o: In function `ZN4CApp9OnCleanupEv': C:/Users/User/Documents/Coding/SDLTesting/CApp_OnCleanup.cpp:5: undefined reference to `SDL\_Quit’  
> obj\Debug\CApp\_OnInit.o: In function `ZN4CApp6OnInitEv': C:/Users/User/Documents/Coding/SDLTesting/CApp_OnInit.cpp:5: undefined reference to `SDL\_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 to `WinMain 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)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

–  
_Pallav Nawani_  
_Game Designer/CEO_

> **[Ironcode Gaming](https://ironcode.com/)**
>
> Founded in 2004 Ironcode is an independent developer and publisher of quality games. Here at Ironcode we develop and produce original games and also provide end

Twitter: [x.com](http://twitter.com/Ironcode_Gaming)  
Facebook: [Ironcode Gaming](http://www.facebook.com/Ironcode.Gaming)

---

<div class="post-metadata">

**Author:** ![Kaifory](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/dfb087/32.png) [@Kaifory](https://discourse.libsdl.org/u/Kaifory)\
**Post date:** [September 5, 2013, 6:28am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/7 "2013-09-05T06:28:58Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Kaifory](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/dfb087/32.png) [@Kaifory](https://discourse.libsdl.org/u/Kaifory)\
**Post date:** [September 5, 2013, 6:41am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/8 "2013-09-05T06:41:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aggelos\_Kolaitis](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@Aggelos\_Kolaitis](https://discourse.libsdl.org/u/Aggelos_Kolaitis)\
**Post date:** [September 5, 2013, 8:00am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/9 "2013-09-05T08:00:22Z")

</div>

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](http://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)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Kaifory](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/dfb087/32.png) [@Kaifory](https://discourse.libsdl.org/u/Kaifory)\
**Post date:** [September 5, 2013, 9:10am UTC](https://discourse.libsdl.org/t/sdl2-errors-and-installation-help/19944/10 "2013-09-05T09:10:42Z")

</div>

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