3

Projects that use the QtOpenGL fail to link to gl calls, but only on Windows (Linux is happy). The MSVC 2008 error messages for the following minimal case project are:

1>gllink.obj : error LNK2019: unresolved external symbol __imp__glLoadIdentity@0 referenced in function "protected: virtual void __thiscall ImageWidget::initializeGL(void)" (?initializeGL@ImageWidget@@MAEXXZ)
1>gllink.obj : error LNK2019: unresolved external symbol __imp__glMatrixMode@4 referenced in function "protected: virtual void __thiscall ImageWidget::initializeGL(void)" (?initializeGL@ImageWidget@@MAEXXZ)
1>gllink.exe : fatal error LNK1120: 2 unresolved externals

These errors disappear when I manually add "opengl32.lib" to the "Additional Dependencies" list, but I believe this should not be necessary, since this should either be done by FindQt4.cmake, or should be taken care of by the dependency of QtOpenGL on opengl32. Of course, I must be mistaken, so I would really appreciate some input on how to properly fix this project.

gllink.cpp

#include <QtOpenGL>
#include <QWidget>

class ImageWidget : public QGLWidget
{
public:
   ImageWidget(QWidget* parent = 0) :
      QGLWidget(parent)
   {
   }

protected:
   void initializeGL()
   {
      glMatrixMode( GL_MODELVIEW );
      glLoadIdentity();
   }
};

int main()
{
   ImageWidget w;
   return 0;
}



CMakeLists.txt

PROJECT( gllink )
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

FIND_PACKAGE( Qt4 4.6.0 REQUIRED COMPONENTS QtCore QtGui QtOpenGL )
INCLUDE( ${QT_USE_FILE} )

ADD_EXECUTABLE( gllink gllink.cpp )
TARGET_LINK_LIBRARIES( gllink ${QT_LIBRARIES} )
4

1 回答 1

3

FindQt4 不处理这个,你必须自己搜索 OpenGL。现在 CMake 提供了一个 FindOpenGL 包,因此修复您的项目只需添加find_package( OpenGL )库并将其链接到您的目标:

PROJECT( gllink )
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

FIND_PACKAGE( OpenGL )

FIND_PACKAGE( Qt4 4.6.0 REQUIRED QtCore QtGui QtOpenGL )
INCLUDE( ${QT_USE_FILE} )

ADD_EXECUTABLE( gllink gllink.cpp )
TARGET_LINK_LIBRARIES( gllink ${OPENGL_LIBRARIES} ${QT_LIBRARIES} )

顺便说一句,您应该检查是否真的找到了这些库,如果没有,则警告用户该问题:)

于 2013-06-11T08:14:57.543 回答