【发布时间】:2010-09-21 00:11:48
【问题描述】:
我刚开始尝试在 C++ 中将 OpenGL 用于一个类(我之前在 Java 中使用过很多次)。我开始尝试写一些实质性的东西,但我无法阻止 Seg 故障,所以我写了这段小代码,几乎是红皮书第一章示例中的一行复制代码。它还有 Seg 故障。我的问题是为什么。我已经尝试过 eclipse 和 netbeans,我的项目中都链接了 glut.h 库,我在使用 VMWare 的虚拟机上运行 64 位 ubuntu 10.4,都安装了 gcc 和 freeglut,netbeans 和 eclipse 都会运行我编写的常规(非 OpenGL)C++ 代码,而不会出现段错误。
这里是代码:
#include <stdlib.h>
#include <GL/freeglut.h>
#include <stdio.h>
void init(){
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75,0.25,0.0);
glVertex3f(0.75,0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250,250); //if I comment out this line,
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]); //this line,
init(); //this line and the glut main loop line it runs without any errors, but why wouldn't it? It's not doing anything now!
glutDisplayFunc(display);
glutMainLoop(); //if I comment out just this line I get illegal instruction instead of segfault but I need this line
return 0;
}
线程 [1] 28944(暂停:信号:SIGSEGV:分段错误)
XF86DRIQueryVersion() 在 0x7ffff7e7412e XF86DRIQueryExtension() 在 0x7ffff7e742c9 0x7ffff7e73c70 0x7ffff7e53ff8 glXGetFBConfigs() 在 0x7ffff7e4c71e glXChooseFBConfigSGIX() 在 0x7ffff7e4cd97
fgChooseFBConfig() at freeglut_window.c:205 0x7ffff794a8c7
fgOpenWindow() at freeglut_window.c:768 0x7ffff794aac8
fgCreateWindow() 在 freeglut_structure.c:106 0x7ffff7948f62 glutCreateWindow() at freeglut_window.c:1,183 0x7ffff794a2a2 main() 在线程 [1] 28944(暂停:信号:SIGSEGV:分段错误) XF86DRIQueryVersion() 在 0x7ffff7e7412e XF86DRIQueryExtension() 在 0x7ffff7e742c9 0x7ffff7e73c70 0x7ffff7e53ff8 glXGetFBConfigs() 在 0x7ffff7e4c71e glXChooseFBConfigSGIX() 在 0x7ffff7e4cd97
fgChooseFBConfig() at freeglut_window.c:205 0x7ffff794a8c7
fgOpenWindow() at freeglut_window.c:768 0x7ffff794aac8
fgCreateWindow() 在 freeglut_structure.c:106 0x7ffff7948f62 glutCreateWindow() at freeglut_window.c:1,183 0x7ffff794a2a2 main() 在(这里的项目资料):54 0x40100b
【问题讨论】:
-
我不确定从标题中编辑 OpenGL 是否会对 BioBuckyBall 有很大帮助,因为我指定 Ubuntu 仅在包含 OpenGL 元素时才不会与基本 C++ 代码发生段错误。因此,虽然您简化了标题,但也降低了它的准确性。
-
发生段错误的堆栈跟踪是什么?你检查过
glGetError()吗? -
其他 OpenGL 代码是否在同一虚拟机中运行? glxinfo 的输出是什么?
-
当你运行可执行文件时,你传递了什么参数?
-
确定不完全确定如何获取段错误的堆栈跟踪,但 Eclipse 告诉我运行调试模式时没有可用于“0x7ffff7e73c70”的源
标签: c++ opengl ubuntu segmentation-fault