Setting Up SDL for C++ with Eclipse on Windows

This article contains very detailed instructions for setting up SDL with MinGW and Eclipse on Windows, as of 8th December 2014. Thanks to Todd Mowery for providing this information.


It is understood that you must previously have MinGW and Eclipse installed prior to using these instructions.

 

I have only tested this on Windows 8.1 (64bit) with Eclipse Luna 4.4.1 using MinGW 32, but may work with other versions of each. Note that for MinGW 64 you will need to use the 64 bit version of SDL2 included in the download from their site.

 

  1. Download SDL2 Development Libraries for MinGW: http://libsdl.org/download-2.0.php Current Direct Link to 2.0.3: http://libsdl.org/release/SDL2-devel-2.0.3-mingw.tar.gz
  2. Extract the zip file. You'll need a program like 7Zip or WinRar on Windows.
  3. If you're using MinGW 32 bit you'll need the i686-w64-mingw32 folders contents. For 64 bit MinGW you'll need x86_64-w64-mingw32. (I haven't tested this though!) The following instructions will use only the i686-w64-mingw32 folder name. If you're using 64 bit MinGW simply replace the i686-w64-mingw32 folder name with x86_64-w64-mingw32.
  4. Copy the SDL2 folder from i686-w64-mingw32/include/SDL2 to the include folder of your MinGW install.
    • Example: C:\MinGW\include\SDL2 (All the SDL header files should be found here ie: SDL.h)
    • Note: I'm sure you can use an alternate location, but this will make linking less cumbersome.
    • Note: If using version 2.0.3 copy the above replacement SDL_Platform.h header file into the SDL2 folder overwriting the original one.
  5. Copy the entire contents of the i686-w64-mingw32/lib to the lib folder of your MinGW install. Example: C:\MinGW\lib\ (All the SDL lib files should be found here ie. libSDL2.a libSDL2main.a)
    • Note: I'm sure you can use an alternate location, but this will make linking less cumbersome.
  6. Start Eclipse and select File->New->C++ Project
  7. Give the Project a name. Example: SDLTest
  8. Under Project type select Hello World C++ Project and under Toolchains select MinGW GCC.
  9. Click Next.
  10. Fill out the Basic Settings if you wish and Click Next.
  11. Make sure that both the Debug and Release checkboxes are checked.
  12. Click Advanced Settings. (Note you can do the next few steps after you finish with the setup also by right clicking the project in the Project Explorer and then selecting Properties.)
  13. In the left hand pane expand C/C++ Build and select Settings.
  14. In the right hand pane towards the top find Configuration and select [All Configurations] from the drop down menu.
  15. Select the Tool Settings tab and expand MinGW C++ Linker if not already expanded.
  16. Under MinGW C++ Linker select Libraries and then in the top right pane (Libraries (-l)) click the Add Icon (looks like a piece of paper with a + on it).
    • Note: If you chose to put your libraries in a location other than the one described in these instructions you will most likely need to add the Library search path (-L) as well.
  17. In the following order add these libraries (case sensitive).
    • mingw32
    • SDL2main
    • SDL2
    You can use the arrows to move them up or down if needed.
  18. (This part (18-22) for the SDL includes is optional, but recommended. If you decided to skip it you'll need to declare the include as #include <SDL2/SDL.h> instead of <SDL.h>) Still in the Tool Settings tab. Expand GCC C++ Compiler if not already expanded. Under GCC C++ Compiler select Includes.
  19. In the top right pane (Include paths (-l)) click the Add Icon (looks like a piece of paper with a + on it).
  20. Use the dialog to add the path to the SDL2 folder where you put your header files.
    • Example: Click the File System button and navigate to C:\MinGW\include\ and click the SDL2 folder and then OK OK.
    • You can also just add the path manually: C:\MinGW\include\SDL2
  21. Scroll down and Click Apply in the Tool Settings tab and then OK to close the Properties window.
  22. Click Finish.
  23. With your new SDL project selected in the left hand pane, from the Eclipse menu select Project->Build Configurations->Set Active->Debug
  24. Now build your Project. Project->Build All or Cntrl+B. You should not have any errors building.
  25. Run your Hello World Project.
  26. Repeat Steps 23-25 this time choosing Release instead of Debug.
  27. Go back to your downloaded version of SDL and in your i686-w64-mingw32\bin folder find the SDL2.dll and copy this to you projects Debug and Release folders next to the exe's you just created. This can be done by right clicking on the SDL2.dll and selecting copy and then selecting your projects Debug folder, right click and select Paste. Repeat for the Release Folder.
    • (Note: without the dll you will not be able to run your project without having it crash if there are any calls to SDL code!! Though it should build! )
  28. Now add #include <SDL.h> to your projects cpp file.
  29. You need to change the main functions signature to match: int main(int argc, char* argv[]); Don't forget to save!
  30. Switch your project back to Debug if you need and then build and run your project again.
  31. Now, provided you don't have any errors you are ready to add your SDL code!!

 

Example project:

#include <SDL.h>
 
int main(int argc, char* argv[]) {
    // Start SDL2
    SDL_Init(SDL_INIT_EVERYTHING);
 
    // Create a Window in the middle of the screen
    SDL_Window *window = 0;
 
    window = SDL_CreateWindow("Hello World!",
                              SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED,
                              640, 480,
                              SDL_WINDOW_SHOWN);
 
    // Delay so that we can see the window appear
    SDL_Delay(2000);
 
    // Cleanup and Quit
    SDL_DestroyWindow(window);
    SDL_Quit();
 
    return 0;
}