-2

i'm working in Linux on an OpenGL project. But when i'm trying to create the window with SDL2, here the output I get :

g++ -lGL -lGLEW -lSDL2 main.cpp display.cpp 
/tmp/cchQfbLR.o: In function `Display::Display(unsigned long, unsigned long, std::string const&)':
display.cpp:(.text+0x21): undefined reference to `SDL_Init'
display.cpp:(.text+0x30): undefined reference to `SDL_GL_SetAttribute'
display.cpp:(.text+0x3f): undefined reference to `SDL_GL_SetAttribute'
display.cpp:(.text+0x4e): undefined reference to `SDL_GL_SetAttribute'
display.cpp:(.text+0x5d): undefined reference to `SDL_GL_SetAttribute'
display.cpp:(.text+0x6c): undefined reference to `SDL_GL_SetAttribute'
/tmp/cchQfbLR.o:display.cpp:(.text+0x7b): more undefined references to `SDL_GL_SetAttribute' follow
/tmp/cchQfbLR.o: In function `Display::Display(unsigned long, unsigned long, std::string const&)':
display.cpp:(.text+0xb1): undefined reference to `SDL_CreateWindow'
display.cpp:(.text+0xc9): undefined reference to `SDL_GL_CreateContext'
display.cpp:(.text+0xd6): undefined reference to `glewInit'
/tmp/cchQfbLR.o: In function `Display::update()':
display.cpp:(.text+0x152): undefined reference to `SDL_PollEvent'
/tmp/cchQfbLR.o: In function `Display::swapBuffers()':
display.cpp:(.text+0x18e): undefined reference to `SDL_GL_SwapWindow'
/tmp/cchQfbLR.o: In function `Display::clearScreen(float, float, float, float)':
display.cpp:(.text+0x1e1): undefined reference to `glClearColor'
display.cpp:(.text+0x1eb): undefined reference to `glClear'
/tmp/cchQfbLR.o: In function `Display::~Display()':
display.cpp:(.text+0x20a): undefined reference to `SDL_GL_DeleteContext'
display.cpp:(.text+0x21a): undefined reference to `SDL_DestroyWindow'
display.cpp:(.text+0x21f): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status

The thing is I've used the exact same code in code::blocks before, and it worked. I searched in other subjects about the same error and found out it maybe have something to do with the linking. But I can't see what i'm doing wrong with. Here how I compile my project :

g++ -lGL -lGLEW -lSDL2 main.cpp display.cpp -o main.o

and here are the files : main.cpp

#include <iostream>
#include "display.h"

int main(int argc, char** argv){
    Display display(800, 600, "engine");

    while(!display.isClosed()){
        display.clearScreen(.1f,.1f,.6f,1.f);

        display.update();
    }
    std::cout << "Hello world !" << std::endl;
    return 0;
}

display.h

#ifndef DISPLAY_H
#define DISPLAY_H

#include <string>
#include <SDL2/SDL.h>

class Display
{
public:
    Display(size_t _width, size_t _height, const std::string& _title);
    void update(); 
    void swapBuffers(); 
    void clearScreen(float _r, float _g, float _b, float _a); 
    ~Display();
    bool isClosed() const {return closed;}
private:
    bool closed; 
    SDL_Window* mWindow; 
    SDL_GLContext mContext; 
};


#endif

display.cpp

#include "display.h"
#include <GL/glew.h>
#include <iostream>

Display::Display(size_t _width, size_t _height, const std::string& _title){
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_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);

    mWindow = SDL_CreateWindow(_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _width, _height, SDL_WINDOW_OPENGL);
    mContext = SDL_GL_CreateContext(mWindow);
    GLuint status = glewInit(); 
    if (status != GLEW_OK){
        std::cerr << "Failed to link the glew library..." << std::endl;
    }
    closed = false; 

}
void Display::update(){
    swapBuffers(); 
    SDL_Event e; 
    while(SDL_PollEvent(&e)){
        if(e.type == SDL_QUIT)
            closed = true; 
    }
} 
void Display::swapBuffers(){
    SDL_GL_SwapWindow(mWindow);
} 
void Display::clearScreen(float _r, float _g, float _b, float _a){
    glClearColor(_r, _g, _b, _a);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} 
Display::~Display(){
    SDL_GL_DeleteContext(mContext);
    SDL_DestroyWindow(mWindow);
    SDL_Quit(); 
}

Thank for your time !

R00t
  • 186
  • 1
  • 2
  • 14
  • 1
    @πάντα ῥεῖ: I have a hard time believing that a question with 14 answers is a duplicate of a question like this. It feels like a quick catch-all that is quite possibly too polluted with information to be of any use. In fact, there is a much more relevant answer that just barely squeaked in under the radar before the question was closed. None of the 14 answers in the "duplicate" question mention the order of the parameters to g++ as a cause of the problem. – Andon M. Coleman Dec 14 '14 at 21:11

1 Answers1

1

put the various -l flags at the end of the command line.

Photon
  • 3,182
  • 1
  • 15
  • 16