使用QT和OpenGL编译C++应用程序时出现问题

3

我正在按照这个教程使用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或Codeblocks使用的一些东西,但我没有使用过这两者。

请问正确的命令行标志是什么,以便我可以编译这个应用程序?

以下是我目前使用的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
4个回答

7

.pro文件是QMake所需的,它是Qt附带的make应用程序所需的文件。该pro文件指定了构建项目所需的所有文件,以及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 \

2

看起来你遇到了链接错误。虽然我没有使用过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库的开发包,在这种情况下,你的系统上很可能有pkg-config文件,即.pc文件,在/usr/lib/pkgconfig目录下。尝试按以下方式编译(未经测试):

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

希望这能帮到你!

1
我建议您使用qmake为您的Qt安装生成正确的Makefile。使用QtCreator可以简化此过程。编写一个只有5行代码的.pro文件,并记得添加QT += opengl。然后运行qmake和make命令。

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接