I'm following this tutorial for building a simple OpenGl application in C++ using QT.
I'm trying to compile the application using g++ and the command line but I get the following errors:
/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
I've tried searching for a solution but all the solutions I can suggest altering a .pro
file which I assume is something used by Netbeans or Codeblocks, neither of which I'm using.
What are the correct command line flags to pass to g++ so I can compile this application?
Here is the g++ command I'm currently using:
g++ *.cpp -I/usr/include/qt4 -I/usr/include/qt4/Qt -I/usr/include/QtCore -I/usr/include/QtGui -lGL
Edit: I ended up using a project file that looks like this.
TEMPLATE = app
TARGET = glQt
QT += opengl
HEADERS += \
GLWidget.h
SOURCES += \
GLWidget.cpp \
main.cpp
The .pro file is needed by QMake, the make app distributed with Qt. This pro file specifies all the files needed to build your project, as well as the Qt modules.
More about qmake and the .pro file here: http://qt-project.org/doc/qt-4.8/qmake-manual.html
If you install the IDE QtCreator, the task of modifying the .pro file and compiling your app will be a bit easier.
After you modify the .pro file, you usually build the Qt app using the command qmake, which produces a make file ready to be used by your system (on Windows it can also produce .sln solutions for Visual Studio)
A short .pro file from one of my projects:
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 \
It looks like you are having a linker error. Although I have not used Qt
, but from the way you compiling it looks like you are not linking Qt
libraries. Try linking Qt
libraries while building your example as follows (untested):
g++ *.cpp -I./ -I/usr/include/qt4 -I/usr/include/qt4/Qt -I/usr/include/QtCore -I/usr/include/QtGui -lGL -lQtCore -lQtGui -lQtOpenGL
I am guessing you must have installed development packages for Qt
libraries in which case most likely you will have pkg-config files
i.e. .pc
files on you system in /usr/lib/pkgconfig
. Try compiling as follows (untested):
g++ *.cpp -I./ `pkg-config --cflags --libs QtOpenGL`
Hope this helps!
I suggest you use qmake to generate a correct Makefile for your Qt installation. Using QtCreator simplifies this. Write a .pro file which is 5 lines of code and remember to add QT += opengl. Then run qmake and make.
I HIGHLY recommend you use qmake! Otherwise you will have to manually call the moc
, uic
, and rcc
yourself!
精彩评论