简单的 OpenGL 代码总是会导致分段错误(Ubuntu、虚拟机上的 C++)
我刚刚开始尝试在 C++ 中使用 OpenGL 来创建一个类(我之前在 Java 中使用过相当多)。我开始尝试写一些实质性的东西,但我无法阻止 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() 位于 freeglu_window.c:205 0x7ffff794a8c7
fgOpenWindow() 位于 freeglut_window.c:768 0x7ffff794aac8
fgCreateWindow() 位于 freeglut_struct.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() 位于 freeglu_window.c:205 0x7ffff794a8c7
fgOpenWindow() 位于 freeglut_window.c:768 0x7ffff794aac8
fgCreateWindow() 位于 freeglut_struct.c:106 0x7ffff7948f62 glutCreateWindow() at freeglut_window.c:1,183 0x7ffff794a2a2 main() 在(此处的项目内容):54 0x40100b
I just started trying to use OpenGL in C++ for a class(I have previously used it in Java a fair amount). And I started off trying to write something substantial, I couldn't get that to stop Seg faulting so I wrote this piddly little piece of code which is nearly a line for line copy from an example in the first chapter of the red book. It also Seg faults. My question is why. I have tried both eclipse, and netbeans, I have the glut.h library linked in my projects in both, I am running 64 bit ubuntu 10.4, on a virtual machine using VMWare, gcc and freeglut are both installed, Both netbeans and eclipse will run regular (non OpenGL) C++ code I write without seg faulting.
Anyway here is the code:
#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;
}
Thread [1] 28944 (Suspended : Signal : SIGSEGV:Segmentation fault)
XF86DRIQueryVersion() at 0x7ffff7e7412e XF86DRIQueryExtension() at
0x7ffff7e742c9 0x7ffff7e73c70 0x7ffff7e53ff8 glXGetFBConfigs()
at 0x7ffff7e4c71e glXChooseFBConfigSGIX() at 0x7ffff7e4cd97
fgChooseFBConfig() at freeglut_window.c:205 0x7ffff794a8c7
fgOpenWindow() at freeglut_window.c:768 0x7ffff794aac8
fgCreateWindow() at freeglut_structure.c:106 0x7ffff7948f62
glutCreateWindow() at freeglut_window.c:1,183 0x7ffff794a2a2 main()
at Thread [1] 28944 (Suspended : Signal : SIGSEGV:Segmentation fault)
XF86DRIQueryVersion() at 0x7ffff7e7412e XF86DRIQueryExtension() at
0x7ffff7e742c9 0x7ffff7e73c70 0x7ffff7e53ff8 glXGetFBConfigs()
at 0x7ffff7e4c71e glXChooseFBConfigSGIX() at 0x7ffff7e4cd97
fgChooseFBConfig() at freeglut_window.c:205 0x7ffff794a8c7
fgOpenWindow() at freeglut_window.c:768 0x7ffff794aac8
fgCreateWindow() at freeglut_structure.c:106 0x7ffff7948f62
glutCreateWindow() at freeglut_window.c:1,183 0x7ffff794a2a2 main()
at (project stuff here):54 0x40100b
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以帮助:
在 glutInitDisplayMode 中添加 GLUT_DEPTH
使用这个标志对我有用,而不是没有( glutCreateWindow 的段错误)
Could help :
add GLUT_DEPTH in glutInitDisplayMode
Works for me with this flag, and not without (segfault at glutCreateWindow)
虚拟机内部有硬件加速吗?使用 glxinfo 检查。 DRI 内部的崩溃表明您不这样做。
Do you have hardware acceleration inside the virtual machine? Check using glxinfo. The crash inside the DRI suggests that you do not.
我使用不同的代码并从 Mac OS X 10.6 远程使用 Debian 6.0 时遇到了类似的问题。有问题的机器使用 Radeon HD 5670 卡(尽管这应该不相关)。
经过仔细检查,OpenGL 本身似乎无法正常工作,因为失败的不仅仅是我的代码。
我遇到的一些症状包括:
对我来说,解决方案是删除 Radeon fglrx 驱动程序(删除所有具有 aptitude 的软件包)并安装“radeon”司机代替。然后运行“sudo Xorg -configure”生成新配置,使用“sudo X -config /root/xorg.conf.new”对其进行测试,并将其复制到默认的 xorg.conf 位置。
之后,程序不再因 SIGSEGV 崩溃,尽管我收到了不同的错误:
这似乎与直接渲染 (DRI) 带来的麻烦有关。解决方案是通过设置“export LIBGL_ALWAYS_INDIRECT=yes”来禁用直接渲染。另一种方法是删除用于 DRI 的库(在我的例子中是 libgl1-mesa-dri)。
我仍然不太相信这是完整的解决方案,因为当仅从远程终端使用 glxinfo 和 glxgears 时,fglxr 驱动程序应该无关紧要。我怀疑删除 fglxr 并安装 radeon 以某种方式解决了配置中的某些问题。
我使用的一些参考包括:
I had a similar problem with different code, and using Debian 6.0 remotely from Mac OS X 10.6. The machine in question was using a Radeon HD 5670 card (although this should not be relevant).
Upon closer inspection it seemed that something with OpenGL itself was not working properly, because it was not only my code that failed.
Some of the symptoms I encountered include:
For me the solution was to remove the Radeon fglrx driver (remove all packages with aptitude) and install the 'radeon' driver instead. Then run 'sudo Xorg -configure' to generate a new configuration, test it with 'sudo X -config /root/xorg.conf.new' and copy it to your default xorg.conf location.
After that the the programs did not crash with SIGSEGV anymore, although I got a different error:
This in turn seemed to be related to direct rendering (DRI) giving trouble. A solution to this was to disable direct rendering by setting 'export LIBGL_ALWAYS_INDIRECT=yes'. An alternative is to remove the library used for DRI (in my case this was libgl1-mesa-dri).
I'm still not quite convinced that this is the whole solution, since the fglxr drivers shouldn't matter when using only glxinfo and glxgears from a remote terminal. I suspect that removing fglxr and installing radeon somehow solved something in the configuration.
Some references I used include:
我的预感是,如果您编译以下精简示例,您仍然会遇到分段错误:
因为您的分段错误发生在:
换句话说,它无法创建窗口(无论其中有什么) 。我的猜测是,按照@Matias 的回答,您需要在虚拟机中启用 3D 加速?但这可能是显示驱动程序问题(32 位与 64 位),或者是一些更危险的问题,例如 freeglut 与 64 位之间的不匹配。你的 OpenGL 版本——因此我之前提出了一系列问题。您能重新编译这个精简版本并发布结果吗?
My hunch is that if you compile the following pared down example, you'll still get a segmentation fault:
Because your segmentation fault is occurring in:
In other words, it can't create a window (doesn't matter what's in it). My guess, along the lines of what @Matias replied is that you need to enable 3D acceleration within your VM? But it might be a display driver issue, 32 vs 64 bit, or something slightly more sinister such as a mis-match between freeglut & your version of OpenGl -- hence my previous barrage of questions. Could you re-compile this pared down version and post the results?