23 Aug 2015

SDL2 - 2.0.3 in NetBeans IDE 8.0.2 configuration tutorial (MinGW/MinGW-W64 + Windows 7)

This post is about my personal experience while configuring the SDL2 (Simple DirectMedia Layer) to compile with MinGW compilers and use in NetBeans IDE 8.0.2 on Windows 7. As a newbie to SDL and NetBeans, I encountered a lot of errors, found solutions and I want to share my gained experience with you. I hope that this tutorial in my post will give some help to someone in need.


Prerequisites for this task

  • Install the MinGW compiler for 32-bit, my install path is C:\Code\mingw\
  • Install the MinGW-W64 compiler for 64-bit, my install path is C:\Code\mingw64\
  • Install the MSYS, my path is C:\Code\msys\1.0
  • Install the CMake, my path is C:\Code\CMake
  • Install the NetBeans IDE 8.0.2 with C/C++ plugin
You should be able to compile and run simple Hello World! example. (To minimize possible initial error, that is not relevant to OpenCV configuration.).


Open the NetBeans IDE and in menu click on Tools/Options. In the C/C++ tab, I have 3 tool collections - Cygwin,


MinGW-W64 


and MinGW, which is set as default.


To add new tool collection and set it correctly, click on button Add, find the bin directory in compilers installation directory and set tool collection like this


I do not say that this is the best setup, but it works in my case.

Note: It is recommended to install MinGW to path without the blank spaces


Hello world project

If everything set, you can now create a new project, in which you will test if the SDL2 is working.


Name the project, in my case it is SDL2project, select save location and MinGW compiler.


Open the main.cpp file and write code for Hello World.


Right-click on project node and select Properties. If you want to apply changes on debug and release at once, set Configuration: to <All configurations>. In Run node selected, set the Console Type to Standard Output. Or, if you want independent console window, set External terminal. This step is not necessary, but I think that this setting is better than Internal Terminal, which is set to default.


In the Build node, select the C++ Compiler and set the Architecture to 32 Bits.


From now on, the applications build under Debug and Release will be 32 bit. Apply the changes. Now we will create the Debug64 and Release64, that will be used for 64 bit applications. In project properties, click on Manage Configurations. and duplicate existing Debug and Release configurations. Rename new condigurations to Debug64 and Release64. 



Since those two new configurations will be defined for x64 architecture, we will use the MinGW-W64 tools. For both new configurations, in Build node set Tool Collection to MinGW-W64 and set individuality the Architecture option in C++ Compiler to 64 Bits.



Now, you should be able to build and run the Debug, Release or Debug64, Release64 with no additional changes and get this output


Now locate the created applications in <ProjectName>/dist folder an try to run them. There should not be problem to run 32 bit versions, but if you try to run 64 bit versions, an error occurs.


Return back to NetBeans IDE and in Build/C++ in Compilation Line section add Additional Options

-static-libgcc -static-libstdc++

to all configurations. Now rebuild under all configurations. Since the code does not contain command to wait for input, the console window closes instantly. It is better to test application in command prompt, you can invoke it by holding Shift + right click on folder containing the application. If everything set and running, you can close the NetBeans IDE for now.

Setting the SDL2

The first thing you need to do is to download the SDL library. I have used the SDL2-2.0.3 version. Libraries are available on project official page:


Download and extract the file where you can find it. I have extracted the contents of the archive into C:\Devel\SDL2-2.0.3 folder. 

32-bit application

Go to folder, where you extracted the SDL2-2.0.3 files and look for folder i686-w64-mingw32. Open the include folder and copy whole folder SDL to MinGW include directory, in my case to C:\Code\mingw\include\. Next, open the lib folder in i686-w64-mingw32 directory and copy two files libSDL2.dll.alibSDL2main.a into MinGW lib directory, in my case to C:\Code\mingw\lib\.

64-bit application

Go to folder, where you extracted the SDL2-2.0.3 files and look for folder x86_64-w64-mingw32. Open the include folder and copy whole folder SDL to MinGW-W64 include directory, in my case to C:\Code\mingw64\x86_64-w64-mingw32\include\. Next, open the lib folder in x86_64-w64-mingw32 directory and copy two files libSDL2.dll.alibSDL2main.a into MinGW-W64 lib directory, in my case to C:\Code\mingw64\x86_64-w64-mingw32\lib\.


Configuring the NetBeans IDE and C++ project

Important - close the NetBeans if you have it running. It will reload includes and libraries in paths that you just added. Run the NetBeans IDE and open Hello world project. Copy following code to main.cpp:


The code contains only main function with basic SDL2 initialization and white 800x600px window centered on screen with white background. After 3 seconds, the window automatically closes. Open the project Properties and you need to go to Build/Linker and set the Additional Options to

-lmingw32 -lSDL2main -lSDL2

in this order. Now you should be able to compile project in all configurations. But to run the application, you need to copy correct SDL2.dll to folder where the application will run.

  • In 32 bit application case, I copied the SDL2.lib from SDL2-2.0.3\i686-w64-mingw32\bin to the project output path for Debug and Release
  • In 64 bit application case, I copied the SDL2.lib from SDL2-2.0.3\x86_64-w64-mingw32\bin to the project output path for Debug64 and Release64.

Now you can compile the project and the result should be looking like this:


with console window behind it. From now on, you can edit the code as you wish. I recommend tutorials on  Lazy Foo' Productions tutorials or browse web.


Possible mistakes and errors

  • Do not mix 32/64 bit architectures. It is painful to locate this errors. 
  • If the #include <SDL2/SDL.h> is underlined, you probably made a mistake when copying files from include or lib from SDL to MinGW folder. Copy folder, not only files in it. 
  • If compiler throws error: undefined reference to `WinMain@16' , you probably forgot to add the -mingw32 option to linker.
  • If you try to compile the code, and get error: winapifamily.h: No such file or directory in SDL_platform.h, you probably have to replace the file located in  mingw/include/SDL2/SDL_platform.h by file SDL_platform.h.
  • If compile is successful, but the application throws the Fatal Error: Out of memory - aborting message, you probably forgot to copy the SDL2.dll library or used wrong version. Replacing with x86 version should resolve the problem.

Additional notes

To run window without console, add additional 

-mwindows -luser32 -lgdi32 -lwinmm -ldxguid 

options to linker. When you run the application in folder, the console window won't appear. If you notice an mistake or missing of important step, or this post helped you in your project, please let me know. Thanks!

3 comments:

  1. no me funciona el código uso codeblocks en ubuntu

    ReplyDelete
  2. Thank you very much !
    Everything is working perfectly now.

    ReplyDelete