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++ 命令:

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

4 回答 4

7

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 \
于 2011-10-04T06:30:48.710 回答
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 files.pc/usr/lib/pkgconfig

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

希望这可以帮助!

于 2011-10-04T04:15:52.370 回答
1

我建议您使用 qmake 为您的 Qt 安装生成正确的 Makefile。使用 QtCreator 可以简化这一点。编写一个 .pro 文件,它是 5 行代码,记得添加 QT += opengl。然后运行 ​​qmake 和 make。

于 2011-10-04T06:32:51.370 回答
0

强烈推荐你使用 qmake!否则你将不得不手动调用moc, uic, 和rcc你自己!

于 2011-10-04T06:30:14.860 回答