Nazarii Plebanskii
Nazarii Plebanskii

Reputation: 353

Undefined reference error in Qt Creator when using SDL and glew

I get error messages, when compiling my code. The output is next:

debug/display.o: In function `ZN7Display5clearEffff':
D:\Qt_Projects\TestProj\build-SomeOpenGLTest-Desktop_Qt_5_5_1_MinGW_32bit-Debug/../SomeOpenGLTest/display.cpp:37: undefined reference to `glClearColor@16'
D:\Qt_Projects\TestProj\build-SomeOpenGLTest-Desktop_Qt_5_5_1_MinGW_32bit-Debug/../SomeOpenGLTest/display.cpp:38: undefined reference to `glClear@4'
D:/Qt_Projects/TestProj/SomeOpenGLTest/lib/SDL2main.lib(./Release/SDL_windows_main.obj):(.text[_main]+0x5): undefined reference to `SDL_SetMainReady'
D:/Qt_Projects/TestProj/SomeOpenGLTest/lib/SDL2main.lib(./Release/SDL_windows_main.obj):(.text[_main]+0x12): undefined reference to `SDL_main'
D:/Qt/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: D:/Qt_Projects/TestProj/SomeOpenGLTest/lib/SDL2main.lib(./Release/SDL_windows_main.obj): bad reloc address 0x8 in section `.text[_WinMain@16]'
collect2.exe: error: ld returned 1 exit status

All I'm trying to do is to connect SDL2 and glew libraries to project and draw some window. My .pro file looks like this:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp \
    display.cpp

INCLUDEPATH += $$PWD/include

LIBS += -L$$PWD/lib \
            -lglew32 \
            -lglew32s \
            -lSDL2 \
            -lSDL2main \
            -lSDL2test

HEADERS += \
    display.h

I've got include folder with all .hpp files from libraries and also got lib folder with all .lib files from libraries. I'm pretty sure, that problem is in linking libraries, but just in case give you my class code:

display.h

#ifndef DISPLAY_H
#define DISPLAY_H

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

class Display
{
public:
    Display(int width = 360, int height = 360, const std::string &title = "Title");
    ~Display();

    void clear(float r, float g, float b, float a);
    void update();
    bool isClosed() const;

private:
    SDL_Window *mWindow;
    SDL_GLContext mGLContext;
    bool mIsClosed;
};

#endif // DISPLAY_H

display.cpp

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

Display::Display(int width, int 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);
    // Context need in order to OpenGL take control of window from OS
    mGLContext = SDL_GL_CreateContext(mWindow);

    GLenum status = glewInit();
    if (status != GLEW_OK)
    {
        std::cerr << "Glew failed to initialize!" << std::endl;
    }
}

Display::~Display()
{
    SDL_GL_DeleteContext(mGLContext);
    SDL_DestroyWindow(mWindow);
    SDL_Quit();
}

void Display::clear(float r, float g, float b, float a)
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);
}

void Display::update()
{
    SDL_GL_SwapWindow(mWindow);

    SDL_Event e;
    while (SDL_PollEvent(&e))
    {
        if (e.type == SDL_QUIT)
        {
            mIsClosed = true;
        }
    }
}

bool Display::isClosed() const
{
    return mIsClosed;
}

Thanks in advance.

Upvotes: 1

Views: 720

Answers (1)

Nazarii Plebanskii
Nazarii Plebanskii

Reputation: 353

In my case, I needed to include this line of code to my .pro file:

LIBS += -L[my_compiler_directory]/lib -lopengl32

By means of this, I got rid of errors, related with undefined reference to `glFunctions'. After that, thanks to answer of gefa, I changed the order of .lib files like this:

LIBS += -L$$PWD/lib \
            -lglew32 \
            -lglew32s \
            -lSDL2main \
            -lSDL2 \
            -lSDL2test

so -lSDL2main is at the top of other SDL2 libs. After that, everything works properly.

Upvotes: 1

Related Questions