简体   繁体   English

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

[英]Problems compiling an c++ application using QT and OpenGL

I'm following this tutorial for building a simple OpenGl application in C++ using QT. 我正在按照本教程使用QT在C ++中构建一个简单的OpenGl应用程序。

I'm trying to compile the application using g++ and the command line but I get the following errors: 我正在尝试使用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

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. 我已经尝试过寻找解决方案,但我建议改变.pro文件的所有解决方案,我认为这是Netbeans或Codeblocks使用的东西,我都没用过。

What are the correct command line flags to pass to g++ so I can compile this application? 要传递给g ++的正确命令行标志是什么,所以我可以编译这个应用程序?

Here is the g++ command I'm currently using: 这是我目前使用的g ++命令:

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. QMake需要.pro文件,这是与Qt一起发布的make app。 This pro file specifies all the files needed to build your project, as well as the Qt modules. 此专业文件指定了构建项目所需的所有文件,以及Qt模块。

More about qmake and the .pro file here: http://qt-project.org/doc/qt-4.8/qmake-manual.html 有关qmake和.pro文件的更多信息,请访问: 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. 如果您安装IDE QtCreator,则修改.pro文件和编译应用程序的任务会更容易一些。

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) 修改.pro文件后,通常使用qmake命令构建Qt应用程序,生成一个准备供系统使用的make文件(在Windows上它也可以为Visual Studio生成.sln解决方案)

A short .pro file from one of my projects: 我的一个项目的简短.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 \

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. 虽然我没有使用过Qt ,但是从你编译它的方式来看,你似乎没有链接Qt库。 Try linking Qt libraries while building your example as follows (untested): 在构建示例时尝试链接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
I am guessing you must have installed development packages for Qt libraries in which case most likely you will have pkg-config files ie .pc files on you system in /usr/lib/pkgconfig . 我猜你必须安装Qt库的开发包,在这种情况下你很可能会在/usr/lib/pkgconfig系统上有pkg-config files.pc文件。 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. 我建议你使用qmake为你的Qt安装生成一个正确的Makefile。 Using QtCreator simplifies this. 使用QtCreator简化了这一点。 Write a .pro file which is 5 lines of code and remember to add QT += opengl. 写一个.pro文件,这是5行代码,记得添加QT + = opengl。 Then run qmake and make. 然后运行qmake并make。

I HIGHLY recommend you use qmake! 强烈建议你使用qmake! Otherwise you will have to manually call the moc , uic , and rcc yourself! 否则你将不得不亲自手动调用mocuicrcc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM