1

I'm having a problem when trying to link SDL2 and GL libraries to Clion (cmake). Despite changing the cmakelists.txt and adding the target-link-libraries() function. So the cmake file looked like this:

cmake_minimum_required(VERSION 3.9)
project(OpenGL1)

set(CMAKE_CXX_STANDARD 17)

target_link_libraries(OpenGL1 SDL2 GL)

add_executable(OpenGL1 main.cpp Display1.cpp Display1.h)

But with no luck. Clion would not link the libraries with errors such as undifined refference to functions of the libraries.

Then a friend suggested it that I would wite that into cmakelists file:

cmake_minimum_required(VERSION 3.9)
project(OpenGL1)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lGL -lSDL2 -lOpenGL")


add_executable(OpenGL1 main.cpp Display1.cpp Display1.h)

And it kinda worked... But it would not find the -lOpenGL. Last but not least, my project will not compile with the traditional way in the terminal with g++. And it will compile fine with one .cpp file. My project consists of 2 cpp files and a header file. Like that:

  • main.cpp
  • Display1.cpp
  • Display.h

And code of the files is the following:

  1. main.cpp

    #include <iostream>
    #include "Display.h"
    
    using namespace std;
    
    int main() {
        Display d1(800,600,"Hello");
    
        while (!d1.m_isClosed()){
            d1.Clear(0.0f, 0.15f, 0.3f, 1.0f);
            d1.Update();
        }
        return 0;
    }
    
  2. Display1.cpp

    #include "Display.h"
    #include <GL/glew.h>
    
    using namespace std;
    
    Display::Display(int width, int height, const string title) {
        SDL_Init(SDL_INIT_EVERYTHING);
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
        SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
    
        m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_OPENGL);
        m_glContext = SDL_GL_CreateContext(m_window);
    
        GLenum status = glewInit();
        if (status != GLEW_OK)
            cerr << "Glew failed!" << endl;
    
        isClosed = false;
    }
    
    Display::~Display() {
        SDL_GL_DeleteContext(m_glContext);
        SDL_DestroyWindow(m_window);
        SDL_Quit();
    }
    
    void Display::Clear(float r, float g, float b, float a) {
        glClearColor(r,g,b,a);
        glClear(GL_COLOR_BUFFER_BIT);
    }
    
    bool Display::m_isClosed() {
        return isClosed;
    }
    
    void Display::Update() {
        SDL_GL_SwapWindow(m_window);
    
        SDL_Event e;
        while (SDL_PollEvent(&e)){
            if (e.type == SDL_QUIT)
                isClosed = true;
        }
    }
    
  3. Display1.h

     #ifndef OPENGL_DISPLAY_H
     #define OPENGL_DISPLAY_H
    
     #include <iostream>
     #include <SDL2/SDL.h>
    
     using namespace std;
    
    
     class Display {
      SDL_Window* m_window;
      SDL_GLContext m_glContext;
      bool isClosed;
     public:
       Display(int width, int height, string title);
       virtual ~Display();
       void Update();
       bool m_isClosed();
       void Clear(float r, float g, float b, float a);
    };
    
    
    #endif //OPENGL_DISPLAY_H
    

Now, i know that others have asked the same, but nothing could help me so that's why I'm asking. I'm running Ubuntu 17.10 (64-bit). Thanks in advance!

Marios Koni
  • 143
  • 1
  • 13
  • You can try using the following FindSDL2 cmake script to find the dependencies for you. An example of linking them to your program can be found in the README: https://github.com/tcbrindle/sdl2-cmake-scripts. You can use the builtin findOpenGL to do the same thing for OpenGL: https://cmake.org/cmake/help/v3.0/module/FindOpenGL.html – Paul Belanger Mar 09 '18 at 15:31
  • Do you get some cmake errors? E.g. `target_link_libraries` should come after `add_executable`. In your order it's wrong. – Thomas Sablik Mar 09 '18 at 15:38
  • You need to find the libraries. Therefor you can use `find_package`, e.g. `find_package(OpenGL)`. Then you need to add the include directories and link the libraries with `include_directories(${OPENGL_INCLUDE_DIRS})` resp. `target_link_libraries(OpenGL1 ${OPENGL_LIBRARIES})`. – Thomas Sablik Mar 09 '18 at 15:45
  • 2
    Possible duplicate of [Using SDL2 with CMake](https://stackoverflow.com/questions/28395833/using-sdl2-with-cmake) – Tsyvarev Mar 09 '18 at 16:50
  • @ThomasSablik That's correct. Wrote it wrong, but I had the correct order despite writing wrong here. – Marios Koni Mar 09 '18 at 16:56

1 Answers1

0

You have to fix your CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(OpenGL1)

set(CMAKE_CXX_STANDARD 17)

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

set(SDL2_INCLUDE_DIRS /usr/include/SDL2)
set(SDL2_LIBRARIES /usr/lib/x86_64-linux-gnu/libSDL2.so)

include_directories(${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS})

add_executable(OpenGL1 main.cpp Display1.cpp Display1.h)

target_link_libraries(OpenGL1 ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${SDL2_LIBRARIES})

First find the OpenGL files with find_package(OpenGL REQUIRED). Then set the paths for SDL2. Set the include directories for your target (OpenGL1). Add your target. Link libraries to your target.

A better way to use SDL2 is to use FindSDL2.cmake and add find_package(SDL2) to your CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(OpenGL1)

set(CMAKE_CXX_STANDARD 17)

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(SDL2 REQUIRED)

include_directories(${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS})

add_executable(OpenGL1 main.cpp Display1.cpp Display1.h)

target_link_libraries(OpenGL1 ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${SDL2_LIBRARIES})

SDL2 installs its own cmake configuration file called sdl2-config.cmake, so you don't need to download it from github.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • Thanks! But got this error: c++: error: $(SDL2_LIBRARY): Δεν υπάρχει τέτοιο αρχείο ή κατάλογος (no such file or directory in greek.) – Marios Koni Mar 09 '18 at 17:04
  • You have to install SDL2 and set the paths yourself. You can search with `find /usr -name libSDL2.so`. On my Ubuntu it is `/usr/lib/x86_64-linux-gnu/libSDL2.so`. When you use FindSDL2.cmake, the script will configure the pass for you. – Thomas Sablik Mar 10 '18 at 11:53
  • I made some steps. I didn't play with "FindSDL2.cmake", but set the paths for the sdl2, opengl directories, I used the find_library function and provided the exact path for both libraries and then linked them but still got the error "/usr/bin/ld: cannot find -lOPENGL". In which I don't have access. – Marios Koni Mar 10 '18 at 12:15
  • Are you sure, that OpenGL is installed on your system? – Thomas Sablik Mar 10 '18 at 12:17
  • Yes, I checked and everything is installed. – Marios Koni Mar 10 '18 at 12:31
  • Can you post your CMakeLists.txt. Do you use `find_package(OpenGL REQUIRED)`? Did you remove `set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lGL -lSDL2 -lOpenGL")`? – Thomas Sablik Mar 10 '18 at 12:43
  • My current version of cmakelists is: cmake_minimum_required(VERSION 3.9) project(OpenGL1) set(CMAKE_CXX_STANDARD 17) find_package(OpenGL REQUIRED) find_package(SDL2 REQUIRED) set(SDL2_INCLUDE_DIR /usr/include/SDL2) set(OPENGL_INCLUDE_DIR /usr/include/GL) set(SDL2_LIBRARY /usr/lib/x86_64-linux-gnu/libSDL2.so) set(OPENGL_LIBRARY /usr/lib/x86_64-linux-gnu/compiz/libopengl.so)${SDL2_INCLUDE_DIR}) link_directories(${OPENGL_INCLUDE_DIR} ${SDL2_INCLUDE_DIR}) add_executable(OpenGL1 main.cpp Display1.cpp Display1.h) target_link_libraries(OpenGL1 SDL2_LIBRARY OPENGL_LIBRARY) – Marios Koni Mar 10 '18 at 12:51
  • Now you are mixing two methods. That can't work. Clear your CMakeLists.txt and copy my second CMakeLists.txt. It should work. You have syntax errors and logical errors in your configuration. – Thomas Sablik Mar 10 '18 at 12:55
  • Ok, I used yours, but got error "undefined reference to `glewInit'". – Marios Koni Mar 10 '18 at 13:47
  • Solved it! Had to include GLEW_LIBRARIES. Thanks for your time! – Marios Koni Mar 10 '18 at 16:29
  • Fixed it in my answer. – Thomas Sablik Mar 10 '18 at 16:39