Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 514
SupineDread89 Avatar asked Nov 16 '25 10:11

SupineDread89


2 Answers

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;
}
like image 138
SupineDread89 Avatar answered Nov 17 '25 23:11

SupineDread89


I can't comment, but I think you should remove the lib before every link lib: target_link_libraries(OpenGLTest SDL2main SDL2 SDL2_test)

(the error message said cannot find -llibSDL2main which the correct flag should be -lSDL2main)

like image 43
tnt Avatar answered Nov 18 '25 00:11

tnt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!