NetBeans 不编译 openGL 项目
Posted
技术标签:
【中文标题】NetBeans 不编译 openGL 项目【英文标题】:NetBeans do not compile openGL project 【发布时间】:2012-02-13 13:37:12 【问题描述】:我在 Netbeans 中编程,使用 MSYS、Qt、mingw 和 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;
【问题讨论】:
【参考方案1】:您的编译(或更具体地说,链接)过程中似乎没有包含 QtOpenGL
模块。
如果您在项目中使用.pro
文件,则需要在其中添加QT += opengl
,以告知qmake
您正在使用QtOpenGL
。其他构建系统可能需要您执行不同的操作。
使用 NetBeans 管理配置时,如果进入项目属性,在 Build -> Qt 下,应该有各种 Qt 模块的复选框,包括 QtOpenGL
。您可以在此处启用/禁用与任何 Qt 模块的链接。
【讨论】:
在NetBeans中,没有.pro文件,不知道怎么把这个语句加到这个文件里 根据输出,它似乎确实使用了qmake
和.pro
文件nbproject/qt-Debug.pro
,至少在内部是这样。如果你进入项目的属性,在 Build -> Qt 下,应该有各种 Qt 模块的复选框,包括QtOpenGL
。
这为我节省了数小时的调试时间!谢谢!以上是关于NetBeans 不编译 openGL 项目的主要内容,如果未能解决你的问题,请参考以下文章
在 NetBeans 中编译 Maven 项目时 Ant 无法找到环境变量
如何强制 Netbeans 仅使用 -ggdb 编译标志而不使用它也自动发出 Makefile-Debug.mk 中的 -g 标志