How to Configure SDL with CLion – On Windows

SDLClion

For the past year or so, I have been working behind the scenes with SDL. While I used Eclipse over the course of the development period, I started leaning more to CLion. Starting in August 2015, I started migrating my projects from Eclipse to CLion. That process wasn’t smooth. As you may or may not know, Eclipse allows you to define the location of library files through a GUI. CLion uses CMake to manage all the linking business. This can be confusing if you have never dealt with CMake before. And so — after a month later, here is how I got SDL and CMake to work together. The source code is also available on github. The link for that is down below.


Gather the Source

Here is what I used, and what you probably will need.

  • CLion v 1.0.5 – but the latest version should work.
  • MinGW x86_64-4.8.3-posix-seh-rt-v3-rev2
  • SDL 2.0.3

Setting up your Project

  • Go ahead and create a new CLion project.
  • In your project directory, create a new folder titled “lib”. Put the library files (.a)  here.
  • Also in your project directory, create a new folder titled “include”. Put your SDL header files here.

You do not have to explicitly name and organize the files as described above. What ever floats your boat.


Configuring CMake

Here we will edit the CMakeList.txt file. 

cmake

If you are unfamiliar with CMake this can look daunting, so I will try to explain what is going on here.

  • set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32”)
    • This line tells the compiler that we want to use C++11, and we want to link it to the mingw32 libraries. This is equivalent to calling up “g++ tar.out main.cpp -lmingw32”
  • set(CMAKE_EXE_LINKER_FLAGS “-static-libgcc -static-libstdc++”)
    • This line is necessary when compiling as C++.
  • include_directories(${PROJECT_SOURCE_DIR}/include)
    • Specifying where our SDL header files are located in relation to the project.
  • link_directories(${PROJECT_SOURCE_DIR}/lib)
    • Specifying where our SDL lib files are located in relation to the project.
  • target_link_libraries(SDL_Demo SDL2main SDL2)
    • This does the actual linking between our project and SDL2 libs.

And now, we test.


Sample File

Going back into your main.cpp file, go ahead and write the following code.

sampleMain

If you followed the steps above, the application should compile. However, when you try and run the program, you are met with an error. You need to move the .dll files to the build directory. If you did not specify the build directory, cmake will generate one for you. When you attempt to run, the output panel on the bottom will say something like:

C:\Users\NDanq\.clion10\system\cmake\generated\1c532e7e\1c532e7e\Debug\SDL_Demo.exe

Go to that directory and drop the SDL.dll in there. When you try and run it again… it should run.


You can also find the project on github: https://github.com/DanqueDynasty/SDL_Demo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s