编译代码时收到错误消息。输出是下一个:
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
我所要做的就是连接 SDL2 和 glew 库来投影和绘制一些窗口。我的 .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
我的 include 文件夹包含来自库的所有 .hpp 文件,还有 lib 文件夹,所有 .lib < / em>来自库的文件。
我很确定,这个问题出现在连接库中,但以防万一给你我的班级代码:
的 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 :(得分: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库的顶部。之后,一切正常。