2012-02-13 157 views
1

我在Netbeans中編程,使用MSYS,Qt,mingw和openGL,並想知道如何解決鏈接器問題。NetBeans不編譯openGL項目

myWindow.h

#ifndef _MYWINDOW_H 
#define _MYWINDOW_H 

#include <QtOpenGL/QGLWidget> 

class myWindow : public QGLWidget{ 
public: 
    myWindow(QGLWidget* parent = 0); 
private: 
    void initializeGL(); 
    void paintGL(); 
    void resizeGL(int w, int h); 
    void drawCircle(int tr_x, int tr_y, float size); 
}; 

#endif /* _MYWINDOW_H */ 

myWindow.cpp

#include "myWindow.h" 
#include <QtGui/QtGui> 
#include <GL/glu.h> 
#include <GL/gl.h> 

myWindow::myWindow(QGLWidget* parent):QGLWidget(parent){ 
    setWindowTitle("lab1"); 
} 

GLfloat circle[20][2] = { 
{1, 0}, {0.951057, 0.309017}, {0.809017, 0.587785}, {0.587786, 0.809017}, 
{0.309018, 0.951056}, {0, 1}, {-0.309016, 0.951057}, {-0.587784, 0.809018}, 
{-0.809016, 0.587787}, {-0.951056, 0.309019},{-1, 0}, {-0.951057, -0.309014}, 
{-0.809019, -0.587783}, {-0.587788, -0.809015}, {-0.30902, -0.951055}, {0, -1}, 
{0.309013, -0.951058}, {0.587782, -0.80902}, {0.809014, -0.587789}, {0.951055, -0.309021}}; 

void myWindow::initializeGL(){ 
    qglClearColor(Qt::gray); 
    glEnable(GL_DEPTH_TEST); 
    glShadeModel(GL_FLAT); 
    glEnable(GL_CULL_FACE); 
} 

void myWindow::paintGL(){ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    //glScalef(2, 2, 1); 

    qglColor(Qt::white); 

    renderText(-50, 150 , 0, QString::fromUtf8("This is a snowman!!!"), QFont()); 

    qglColor(Qt::black); 
    drawCircle(10, 75, 3); //eye1 
    drawCircle(-10, 75, 3); // eye2 

    glLineWidth(3.0f); 
    qglColor(Qt::red); 
    glBegin(GL_LINES); 
     glVertex2f(-10, 60); 
     glVertex2f(0, 55); 
     glVertex2f(0, 55); 
     glVertex2f(10, 60); 
    glEnd(); 

    glLineWidth(5.0f); 
    qglColor(Qt::black); 
    glBegin(GL_LINES); 
     glVertex2f(-70, 10); 
     glVertex2f(-38, 5); 

     glVertex2f(38, 5); 
     glVertex2f(70, 10); 
    glEnd(); 

    qglColor(Qt::green); 
    glBegin(GL_POLYGON); 
     glVertex2f(-15, 120); 
     glVertex2f(-20, 90); 
     glVertex2f(20, 90); 
     glVertex2f(15, 120); 
    glEnd(); 

    QColor white(255, 255, 255, 255); 
    qglColor(white); 
    drawCircle(0, 65, 30); 
    drawCircle(0, 0, 40); 
    drawCircle(0, -85, 50); 

    glFlush(); 
} 

void myWindow::resizeGL(int w, int h){ 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    gluOrtho2D(-w/2, w/2, -h/2, h/2); 
    // Update OpenGL viewport and internal variables 
    glViewport(0,0,w,h); 
} 

void myWindow::drawCircle(int tr_x, int tr_y, float size){ 
    int i; 

    glBegin(GL_POLYGON); 
    for(i = 0; i < 20; i++) 
      glVertex2f(circle[i][0]*size+tr_x, circle[i][1]*size+tr_y); 
    glEnd(); 
} 

的main.cpp

#include <QtGui/QApplication> 
#include "myWindow.h" 

int main(int argc, char *argv[]) { 
    // initialize resources, if needed 
    // Q_INIT_RESOURCE(resfile); 

    QApplication app(argc, argv); 

    myWindow win; 
    win.show(); 

    // create and show your widgets here 

    return app.exec(); 
} 

NetBeans的輸出:

/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf 
make[1]: Entering directory `/c/Users/Galym/Documents/NetBeansProjects/qt' 
/C/Qt/4.8.0/bin/qmake.exe VPATH=. -o qttmp-Debug.mk nbproject/qt-Debug.pro 
mv -f qttmp-Debug.mk nbproject/qt-Debug.mk 
/usr/bin/make -f nbproject/qt-Debug.mk dist/Debug/MinGW-Windows/qt.exe 
make[2]: Entering directory `/c/Users/Galym/Documents/NetBeansProjects/qt' 
g++.exe -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/Qt/4.8.0/include/QtCore' -I'c:/Qt/4.8.0/include/QtGui' -I'c:/Qt/4.8.0/include' -I'c:/Qt/4.8.0/include/ActiveQt' -I'.' -I'nbproject' -I'.' -I'c:/Qt/4.8.0/mkspecs/default' -o build/Debug/MinGW-Windows/myWindow.o myWindow.cpp 
g++.exe -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/Qt/4.8.0/include/QtCore' -I'c:/Qt/4.8.0/include/QtGui' -I'c:/Qt/4.8.0/include' -I'c:/Qt/4.8.0/include/ActiveQt' -I'.' -I'nbproject' -I'.' -I'c:/Qt/4.8.0/mkspecs/default' -o build/Debug/MinGW-Windows/main.o main.cpp 
windres -i qt_resource.rc -o build/Debug/MinGW-Windows/qt_resource_res.o --include-dir=. -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN 
g++ -mthreads -Wl,-subsystem,windows -o dist/Debug/MinGW-Windows/qt.exe build/Debug/MinGW-Windows/myWindow.o build/Debug/MinGW-Windows/main.o -L'c:/Qt/4.8.0/lib' -lmingw32 -lqtmaind build/Debug/MinGW-Windows/qt_resource_res.o -lglu32 -lopengl32 -lgdi32 -luser32 -lQtGuid4 -lQtCored4 
build/Debug/MinGW-Windows/myWindow.o: In function `myWindow': 
C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:13: undefined reference to `_imp___ZN9QGLWidgetC2EP7QWidgetPKS_6QFlagsIN2Qt10WindowTypeEE' 
C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:15: undefined reference to `_imp___ZN9QGLWidgetD2Ev' 
C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:13: undefined reference to `_imp___ZN9QGLWidgetC2EP7QWidgetPKS_6QFlagsIN2Qt10WindowTypeEE' 
C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:15: undefined reference to `_imp___ZN9QGLWidgetD2Ev' 
build/Debug/MinGW-Windows/myWindow.o:C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:25: undefined reference to `_imp___ZNK9QGLWidget13qglClearColorERK6QColor' 
build/Debug/MinGW-Windows/myWindow.o:C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:38: undefined reference to `_imp___ZNK9QGLWidget8qglColorERK6QColor' 
build/Debug/MinGW-Windows/myWindow.o:C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:40: undefined reference to `_imp___ZN9QGLWidget10renderTextEdddRK7QStringRK5QFonti' 
build/Debug/MinGW-Windows/myWindow.o:C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:42: undefined reference to `_imp___ZNK9QGLWidget8qglColorERK6QColor' 
build/Debug/MinGW-Windows/myWindow.o:C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:47: undefined reference to `_imp___ZNK9QGLWidget8qglColorERK6QColor' 
build/Debug/MinGW-Windows/myWindow.o:C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:56: undefined reference to `_imp___ZNK9QGLWidget8qglColorERK6QColor' 
build/Debug/MinGW-Windows/myWindow.o:C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:65: undefined reference to `_imp___ZNK9QGLWidget8qglColorERK6QColor' 
build/Debug/MinGW-Windows/myWindow.o:C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.cpp:74: undefined reference to `_imp___ZNK9QGLWidget8qglColorERK6QColor' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x8): undefined reference to `QGLWidget::metaObject() const' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0xc): undefined reference to `QGLWidget::qt_metacast(char const*)' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x10): undefined reference to `QGLWidget::qt_metacall(QMetaObject::Call, int, void**)' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x1c): undefined reference to `QGLWidget::event(QEvent*)' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x54): undefined reference to `QGLWidget::paintEngine() const' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x84): undefined reference to `QGLWidget::paintEvent(QPaintEvent*)' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x8c): undefined reference to `QGLWidget::resizeEvent(QResizeEvent*)' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0xe8): undefined reference to `QGLWidget::updateGL()' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0xec): undefined reference to `QGLWidget::updateOverlayGL()' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0xfc): undefined reference to `QGLWidget::initializeOverlayGL()' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x100): undefined reference to `QGLWidget::resizeOverlayGL(int, int)' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x104): undefined reference to `QGLWidget::paintOverlayGL()' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x108): undefined reference to `QGLWidget::glInit()' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x10c): undefined reference to `QGLWidget::glDraw()' 
build/Debug/MinGW-Windows/myWindow.o:myWindow.cpp:(.rdata$_ZTV8myWindow[vtable for myWindow]+0x124): undefined reference to `non-virtual thunk to QGLWidget::paintEngine() const' 
build/Debug/MinGW-Windows/myWindow.o: In function `~myWindow': 
C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.h:13: undefined reference to `_imp___ZN9QGLWidgetD2Ev' 
C:\Users\Galym\Documents\NetBeansProjects\qt/myWindow.h:13: undefined reference to `_imp___ZN9QGLWidgetD2Ev' 
collect2: выполнение ld завершилось с кодом возврата 1 
make[2]: *** [dist/Debug/MinGW-Windows/qt.exe] Error 1 
make[2]: Leaving directory `/c/Users/Galym/Documents/NetBeansProjects/qt' 
make[1]: *** [.build-conf] Error 2 
make[1]: Leaving directory `/c/Users/Galym/Documents/NetBeansProjects/qt' 
make: *** [.build-impl] Error 2 
BUILD FAILED (exit value 2, total time: 7s) 

環境變量PATH = C:\ mingw \ bin; C:\ Qt \ 4.8.0 \ bin;

回答

2

這似乎是你在編譯(或更具體地說,鏈接)過程中不包括QtOpenGL模塊。

如果您使用.pro文件爲您的項目,你將需要添加QT += opengl吧,告訴你使用QtOpenGLqmake。其他構建系統可能需要你做不同的事情。

當使用NetBeans管理配置時,如果進入項目屬性,在Build - > Qt下,應該有用於各種Qt模塊的複選框,其中包括QtOpenGL。在這裏你可以啓用/禁用鏈接到任何Qt模塊。

+0

在NetBeans中,沒有.pro文件,我不知道如何將此語句添加到此文件 – test1604 2012-02-13 13:48:47

+0

根據輸出結果,它似乎會*使用'qmake'和'.pro 'file'nbproject/qt-Debug.pro',至少在內部。如果你進入項目的屬性,在Build - > Qt下,應該有用於各種Qt模塊的複選框,包括'QtOpenGL'。 – Kitsune 2012-02-13 14:02:42

+0

非常感謝,現在正在工作) – test1604 2012-02-13 14:06:27