簡體   English   中英

使用SDL和glew時Qt Creator中存在未定義的參考錯誤

[英]Undefined reference error in Qt Creator when using SDL and glew

編譯代碼時收到錯誤消息。 輸出是下一個:

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

我要做的就是將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文件的include文件夾,還從庫中包含所有.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;
}

提前致謝。

就我而言,我需要將以下代碼行包含到我的.pro文件中:

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

這樣,我擺脫了與未定義引用glFunctions有關的錯誤。 之后,由於gefa的回答 ,我更改了.lib文件的順序,如下所示:

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

因此-lSDL2main位於其他SDL2庫的頂部。 之后,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM