Search code examples
c++c++11sdlsdl-2clion

How to configure Clion to work with SDL2?


I´m just trying to learn how to use SDL in Clion (the Jetbrains IDE), i found an answer in a post to edit my CMakeLists.txt, i did it!! but it didn´t worked.

I can use the libraries but I can´t compile it, in the image you can see the error in the output console.

Can somebody help me to configure it, please

This is my "CMakeLists.txt"

cmake_minimum_required(VERSION 3.6)
project(OpenGLTest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)

set(SOURCE_FILES main.cpp)
add_executable(OpenGLTest ${SOURCE_FILES})

target_link_libraries(OpenGLTest libSDL2main libSDL2 libSDL2_test)

And this is my "main.cpp"

#include <iostream>
#include <SDL.h>

int main() {
    SDL_Init(SDL_INIT_EVERYTHING);
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

PS: This is the code that I wrote is just to test the compiling.

This is the error in Clion console

enter image description here

Error


Solution

  • The correct text of "CMakeLists.txt" is:

    cmake_minimum_required(VERSION 3.6)
    project(OpenGLTest)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
    set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
    
    include_directories(${PROJECT_SOURCE_DIR}/include)
    link_directories(${PROJECT_SOURCE_DIR}/lib)
    
    set(SOURCE_FILES main.cpp)
    add_executable(OpenGLTest ${SOURCE_FILES})
    
    target_link_libraries(OpenGLTest mingw32 SDL2main SDL2)
    

    And you have to copy the file "SDL2.dll" (located in \bin) to the "cmake-build-debug" folder.

    PS: Don´t forget the "include" and "lib" folders

    This is an example code just to test it.

    #include <iostream>
    #include <SDL.h>
    
    int main(int argc, char* argv []) {
        if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
            std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
            return 1;
        } 
        std::cout << "Hello, World!" << std::endl;
        SDL_Quit();
        return 0;
    }