Search code examples
c++qtopenglqmake

Qt 5.5 with qmake: Linker cannot resolve OpenGL function calls


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

I have:

  • Specified QT += opengl
  • Explicitly specified CONFIG += windows (apparently += console disables gui features)

the .pro file:

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

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 file:

#include <QOpenGLWidget>
#include <QOpenGLFunctions>

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

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

};

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

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.


Solution

  • You need to add in .pro file

    LIBS += opengl32.lib
    

    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