찾다

 >  Q&A  >  본문

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。

黄舟黄舟2768일 전762

모든 응답(3)나는 대답할 것이다

  • 阿神

    阿神2017-04-17 14:01:30

    已解决,参考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
    }

    编译成功。

    회신하다
    0
  • 阿神

    阿神2017-04-17 14:01:30

    如果引用路径都没错的话,可以考虑下图形驱动的问题。好像Windows下Qt的gl库里面某些API是基于DX11(9)实现的,装下DX的SDK试试

    회신하다
    0
  • 阿神

    阿神2017-04-17 14:01:30

    贴出错的图片吧

    회신하다
    0
  • 취소회신하다