c++ - Qt OpenGL 部分函数 在Windows下报错:undefined reference xxx
黄舟
黄舟 2017-04-17 13:59:30
[C++讨论组]

这个问题仅在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。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(3)
阿神

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

编译成功。

阿神

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

阿神

贴出错的图片吧

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号