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

标签 c++ qt opengl g++ qglwidget

我正在关注 this tutorial用于使用 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++ 命令:

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

最佳答案

QMake 需要 .pro 文件,它是与 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 \

关于c++ - 使用 QT 和 OpenGL 编译 C++ 应用程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7642955/

相关文章:

c++ - std::getline 和 eol 与 eof

c++ - 您可以使用 C++/CX 编写普通的 Windows 应用程序吗?

c++ - 使用不同优化级别编译的不同翻译单元中的模板实例化

c++ - Qt 应用程序在与使用 Qt 制作的 DLL 链接时崩溃

c++ - 为轴添加 slider 时未定义对 main 的引用

c++ - 如何正确填充顶点数组

c++ - 分割故障QPainter

c++ - 虚函数查找的规则是什么?

c++ - 控制台上的 Qt 应用程序文本流

c++ - 处理可能依赖的顶点的创建/销毁时的 VBO/VAO 内存管理实践