c++ - Qt OpenGL 部分函数 在Windows下报错:undefined reference xxx

查看:122
本文介绍了c++ - Qt OpenGL 部分函数 在Windows下报错:undefined reference xxx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

这个问题仅在Windows下存在,在Linux和OSX下面是可以正常编译和运行的。

问题出在如下函数:

void GLWidget::drawBackground(QPainter *, const QRectF &)
{
    glClearColor(0, 0, 102 / 255, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glColor3f(0, 0, 0);
    glLineWidth(2);

    float margin = 0.05;
    float l = margin, r = 1 - margin, b = margin, t = 1 - margin;
    int splitNum = 9;
    float dx =(1 - margin * 2) / (splitNum + 1);

    glBegin(GL_LINE_LOOP);
        glVertex2f(l, b);
        glVertex2f(l, t);
        glVertex2f(r, t);
        glVertex2f(r, b);
    glEnd();

    glBegin(GL_LINES);
    for(int i = 1; i <= splitNum; i++)
    {
        glVertex2f(l, b + dx * i);
        glVertex2f(r, b + dx * i);
        glVertex2f(l + dx * i, b);
        glVertex2f(l + dx * i, t);
    }
    glEnd();
    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}

以及(这个是加载obj3D模型用的)

void Model::render(bool wireframe, bool normals) const 
{ 
    glEnable(GL_DEPTH_TEST); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    if (wireframe) { 
        glVertexPointer(3, GL_FLOAT, 0, (float *)m_points.data()); 
        glDrawElements(GL_LINES, m_edgeIndices.size(), GL_UNSIGNED_INT, m_edgeIndices.data()); 
    } else { 
        glEnable(GL_LIGHTING); 
        glEnable(GL_LIGHT0); 
        glEnable(GL_COLOR_MATERIAL); 
        glShadeModel(GL_SMOOTH);

        glEnableClientState(GL_NORMAL_ARRAY); 
        glVertexPointer(3, GL_FLOAT, 0, (float *)m_points.data()); 
        glNormalPointer(GL_FLOAT, 0, (float *)m_normals.data()); 
        glDrawElements(GL_TRIANGLES, m_pointIndices.size(), GL_UNSIGNED_INT, m_pointIndices.data());

        glDisableClientState(GL_NORMAL_ARRAY); 
        glDisable(GL_COLOR_MATERIAL); 
        glDisable(GL_LIGHT0); 
        glDisable(GL_LIGHTING); 
    }

    if (normals) { 
        QVector<Point3d> normals; 
        for (int i = 0; i < m_normals.size(); ++i) 
            normals << m_points.at(i) << (m_points.at(i) + m_normals.at(i) * 0.02f); 
        glVertexPointer(3, GL_FLOAT, 0, (float *)normals.data()); 
        glDrawArrays(GL_LINES, 0, normals.size()); 
    } 
    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisable(GL_DEPTH_TEST); 
}

在Windows下面会出现一堆undefined reference的错误,但在Linux和OSX下面却能正常编译和运行,是Windows需要装哪个依赖包吗?
使用的是Qt5自带OpenGL库,Qt5.6 MinGW 32Bit。

解决方案

已解决,参考Qt5 Docs Windows系统要求

Applications that require a certain OpenGL implementation (for example, desktop OpenGL due to relying on features provided by OpenGL 3.0 or higher) should set the application attributes Qt::AA_UseOpenGLES or Qt::AA_UseDesktopOpenGL before instantiating QGuiApplication or QApplication. When these attributes are set, no other OpenGL implementations are considered. Additionally, if they wish to, such applications are free to make direct OpenGL function calls by adding opengl32.lib to their .pro project files: LIBS += opengl32.lib (Visual Studio) or LIBS += -lopengl32 (MinGW). The result is, from the application's perspective, equivalent to the -opengl desktop build configuration of Qt.

在项目的.pro文件中添加:

win32-g++ {
    LIBS += -lopengl32
}
win32-msvc*{
    LIBS += opengl32.lib
}

编译成功。

这篇关于c++ - Qt OpenGL 部分函数 在Windows下报错:undefined reference xxx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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