2016-02-19 113 views
1

編譯我的代碼時,出現錯誤消息。輸出是下一個:使用SDL和glew時Qt Creator中未定義的參考錯誤

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 `[email protected]' 
D:\Qt_Projects\TestProj\build-SomeOpenGLTest-Desktop_Qt_5_5_1_MinGW_32bit-Debug/../SomeOpenGLTest/display.cpp:38: undefined reference to `[email protected]' 
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[[email protected]]' 
collect2.exe: error: ld returned 1 exit status 

所有我想要做的就是連接SDL2GLEW庫項目,並得出一些窗口。我的.pro文件看起來像這樣:

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 

我有包括文件夾中的所有.HPP從圖書館文件,也得到了的lib文件夾從庫中的所有的.lib文件 。 我敢肯定,這個問題是在鏈接的庫,但以防萬一給你我的類代碼:

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

提前致謝。

+0

你讀過這個問題http://stackoverflow.com/questions/9336263/glfw-undefined-references? – user2807083

+0

@dtbeaver,是的,我做到了。但我不知道,在哪裏可以找到libGL或libGLU以便在.pro文件中設置它們。 –

+0

您是否嘗試簡單地添加-lglew32 -lglfw3 -lopengl32 -lglu32 -lgdi32就像在該答案中推薦的那樣? http://stackoverflow.com/a/17216206/2807083 – user2807083

回答

1

就我而言,我需要加入這一行的代碼我的.pro文件:

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

藉助於此,我擺脫了錯誤,未定義參考`glFunctions'相關。 之後,由於answer of gefa,我改變了的.lib文件的順序是這樣的:

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

所以-​​lSDL2main是在其他SDL2庫的頂部。之後,一切正常。