带有qmake的Qt 5.5:链接器无法解析OpenGL函数调用 [英] Qt 5.5 with qmake: Linker cannot resolve OpenGL function calls

查看:489
本文介绍了带有qmake的Qt 5.5:链接器无法解析OpenGL函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Qt 5.5,qmake和MSVC 13编译具有一些基本OpenGL函数调用的基本样板Qt应用程序时,出现以下链接器错误:

When using Qt 5.5, qmake and MSVC 13 to compile a basic, boilerplate Qt application with some fundamental OpenGL function calls, I get the following linker errors:

glwidget.obj:-1: error: LNK2019: unresolved external symbol __imp__glClear@4 referenced in function "public: virtual void __thiscall GLWidget::initializeGL(void)" (?initializeGL@GLWidget@@UAEXXZ)
glwidget.obj:-1: error: LNK2019: unresolved external symbol __imp__glClearColor@16 referenced in function "public: virtual void __thiscall GLWidget::initializeGL(void)" (?initializeGL@GLWidget@@UAEXXZ)
debug\OpenGLApp.exe:-1: error: LNK1120: 2 unresolved externals

我有:

  • 指定的QT + = opengl
  • 明确指定CONFIG + = Windows(显然+ =控制台禁用gui功能)

.pro文件:

QT       += core gui opengl widgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets opengl

TARGET = OpenGLApp
TEMPLATE = app

CONFIG += windows

SOURCES += main.cpp\
        mainwindow.cpp \
    glwidget.cpp

HEADERS  += mainwindow.h \
    glwidget.h

glwidget.cpp文件:

the glwidget.cpp file:

#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent) {

}

void GLWidget::initializeGL() {
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
}

glwidget.h文件:

glwidget.h file:

#include <QOpenGLWidget>
#include <QOpenGLFunctions>

class GLWidget : public QOpenGLWidget {
    Q_OBJECT
public:    
    GLWidget(QWidget *);

    void initializeGL();
    void resizeGL();
    void PaintGL();

};

在另一个几乎相同的测试程序中,我遇到了相同的问题,即链接器无法解析OpenGL函数调用.通过改用CMake,尤其是下面的"find_package(OpenGL必需)"行,以及在"target_link_libraries"中添加"$ {OPENGL_LIBRARIES}",我能够解决问题:

In another virtually identical test program, I had the same problem of the linker being unable to resolve OpenGL function calls. By using CMake instead, specifically with the following "find_package(OpenGL REQUIRED)" line, and the addition of "${OPENGL_LIBRARIES}" in "target_link_libraries" I was able to solve the problem:

#Qt5
find_package(Qt5Core REQUIRED)

find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5OpenGL REQUIRED)

#OpenGL
find_package(OpenGL REQUIRED)

target_link_libraries(${PROJECT_NAME} Qt5::Widgets Qt5::Gui Qt5::Core Qt5::OpenGL ${OPENGL_LIBRARIES})

因此,我怀疑qmake无法找到OpenGL库,尽管我不确定如何检查以及这可能是什么原因,因此如果有人可以向我指出我所缺少的内容,我将不胜感激. /p>

I therefore suspect qmake is unable to find the OpenGL libraries, although I am unsure as how to check and what may be the cause of this, and so would appreciate if someone could point out to me what I'm missing.

推荐答案

您需要添加.pro文件

You need to add in .pro file

LIBS += opengl32.lib

如果您使用Visual Studio正确链接OpenGL库.

if you are using Visual Studio for correct linking of OpenGL libraries.

您可以在此处找到更多详细信息:

You can find some more details here:

http://doc.qt.io/qt-5/windows- requirements.html

这篇关于带有qmake的Qt 5.5:链接器无法解析OpenGL函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆