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:
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; }
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; } }
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!