使用QT和OpenGL编译c++应用程序的问题

Problems compiling an c++ application using QT and OpenGL

本文关键字:应用程序 问题 c++ 编译 QT OpenGL 使用      更新时间:2023-10-16

我正在遵循这个教程,使用QT在c++中构建一个简单的OpenGl应用程序。

我试图使用g++和命令行编译应用程序,但我得到以下错误:

/tmp/ccH2KFoZ.o: In function `GLWidget::GLWidget(QWidget*)':
GLWidget.cpp:(.text+0x38): undefined reference to `QGLWidget::QGLWidget(QWidget*, QGLWidget const*, QFlags<Qt::WindowType>)'
GLWidget.cpp:(.text+0x41): undefined reference to `vtable for GLWidget'
GLWidget.cpp:(.text+0x4b): undefined reference to `vtable for GLWidget'
GLWidget.cpp:(.text+0x5e): undefined reference to `QGLWidget::setMouseTracking(bool)'
GLWidget.cpp:(.text+0x6f): undefined reference to `QGLWidget::~QGLWidget()'
/tmp/ccH2KFoZ.o: In function `GLWidget::GLWidget(QWidget*)':
GLWidget.cpp:(.text+0xbe): undefined reference to `QGLWidget::QGLWidget(QWidget*, QGLWidget const*, QFlags<Qt::WindowType>)'
GLWidget.cpp:(.text+0xc7): undefined reference to `vtable for GLWidget'
GLWidget.cpp:(.text+0xd1): undefined reference to `vtable for GLWidget'
GLWidget.cpp:(.text+0xe4): undefined reference to `QGLWidget::setMouseTracking(bool)'
GLWidget.cpp:(.text+0xf5): undefined reference to `QGLWidget::~QGLWidget()'
/tmp/ccH2KFoZ.o: In function `GLWidget::resizeGL(int, int)':
GLWidget.cpp:(.text+0x1e1): undefined reference to `gluOrtho2D'
/tmp/ccH2KFoZ.o: In function `GLWidget::keyPressEvent(QKeyEvent*)':
GLWidget.cpp:(.text+0x2dd): undefined reference to `QWidget::close()'
/tmp/ccDIuk1w.o: In function `main':
main.cpp:(.text+0x29): undefined reference to `QApplication::QApplication(int&, char**, int)'
main.cpp:(.text+0x6a): undefined reference to `QApplication::exec()'
main.cpp:(.text+0xa0): undefined reference to `QApplication::~QApplication()'
main.cpp:(.text+0xb8): undefined reference to `QApplication::~QApplication()'
/tmp/ccDIuk1w.o: In function `QWidget::resize(int, int)':
main.cpp:(.text._ZN7QWidget6resizeEii[QWidget::resize(int, int)]+0x2d): undefined reference to `QWidget::resize(QSize const&)'
/tmp/ccDIuk1w.o: In function `GLWidget::~GLWidget()':
main.cpp:(.text._ZN8GLWidgetD1Ev[GLWidget::~GLWidget()]+0x13): undefined reference to `vtable for GLWidget'
main.cpp:(.text._ZN8GLWidgetD1Ev[GLWidget::~GLWidget()]+0x1d): undefined reference to `vtable for GLWidget'
main.cpp:(.text._ZN8GLWidgetD1Ev[GLWidget::~GLWidget()]+0x28): undefined reference to `QGLWidget::~QGLWidget()'
collect2: ld returned 1 exit status

我试着寻找一个解决方案,但所有的解决方案,我可以建议改变一个.pro文件,我认为是Netbeans或代码块使用的东西,这两个我都不使用。

传递给g++的正确命令行标志是什么,以便我可以编译这个应用程序?

下面是我正在使用的g++命令:

g++ *.cpp  -I/usr/include/qt4 -I/usr/include/qt4/Qt -I/usr/include/QtCore -I/usr/include/QtGui -lGL 

编辑:我最终使用了一个项目文件,看起来像这样。

TEMPLATE = app
TARGET = glQt
QT += opengl
HEADERS += 
    GLWidget.h
SOURCES += 
    GLWidget.cpp 
    main.cpp

.pro文件是QMake需要的,QMake是Qt发布的make应用程序。这个概要文件指定了构建项目所需的所有文件,以及Qt模块。

关于qmake和.pro文件的更多信息:http://qt-project.org/doc/qt-4.8/qmake-manual.html

如果你安装了IDE的QtCreator,修改。pro文件和编译你的应用程序的任务会更容易一些。

在你修改了。pro文件之后,你通常使用qmake命令来构建Qt应用程序,它会生成一个make文件,供你的系统使用(在Windows上,它也可以为Visual Studio生成。sln解决方案)

我的一个项目中的一个简短的。pro文件:

TEMPLATE = app
TARGET = icat2browser
QT += network webkit <-- add here the opengl module
DEFINES += QT_LARGEFILE_SUPPORT
HEADERS += 
    qicat2networkaccessmanager.h 
    ctimeline.h 
    cobjectsview.h 
    cicat2WebResponse.h 
    cicat2usersdialog.h 
    cicat2previewdialog.h 
    cicat2login.h 
    cicat2clients.h 
    cicat2browser.h 
SOURCES += 
    qicat2networkaccessmanager.cpp 
    main.cpp 
    ctimeline.cpp 
    cobjectsview.cpp 
    cicat2WebResponse.cpp 
    cicat2usersdialog.cpp 
    cicat2previewdialog.cpp 
    cicat2login.cpp 
    cicat2clients.cpp 
    cicat2browser.cpp 

看起来你有一个链接错误。虽然我没有使用Qt,但从你编译的方式来看,你似乎没有链接Qt库。在构建示例时尝试链接Qt库,如下所示(未测试):
g++ *.cpp -I./ -I/usr/include/qt4 -I/usr/include/qt4/Qt -I/usr/include/QtCore -I/usr/include/QtGui -lGL -lQtCore -lQtGui -lQtOpenGL
我猜你一定已经安装了Qt库的开发包,在这种情况下,你很可能在/usr/lib/pkgconfig的系统上有pkg-config files.pc文件。尝试编译如下代码(未测试):

g++ *.cpp -I./ `pkg-config --cflags --libs QtOpenGL`  

希望这对你有帮助!

我建议您使用qmake为您的Qt安装生成正确的Makefile。使用QtCreator简化了这一点。编写一个.pro文件,它是5行代码,并记住添加QT += opengl。

强烈建议使用qmake!否则,您将不得不自己手动调用mocuicrcc !