我正在尝试编写一个简单的程序,仅使用GLEW 2.0和GLFW 3打开一个窗口,但遇到段错误...
这是我的资料来源:
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int argc, char **argv) {
const int height = 100;
const int width = 100;
if (!glfwInit()) {
printf("Glfw failed to init\n");
return -1;
}
// 4x antialiasing
glfwWindowHint(GLFW_SAMPLES, 4);
// We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// We don't want the old OpenGL
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(width, height, "Tutorial 01", NULL, NULL);
if (window == NULL) {
printf("GLFW Failed to open a window. "
"Intel GPUs don't support 3.3\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = 1;
if (glewInit() != GLEW_OK) {
printf("GLEW Failed to initialize.\n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do {
glfwSwapBuffers(window);
glfwPollEvents();
} while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
}
我用这个编译
gcc test.c -g -lGLEW -lglfw -o test
并在运行该程序时收到段错误。使用
gdb
,段错误的回溯可提供0 0x0000000000000000 in ?? ()
1 0x00007ffff7ba02a7 in glxewInit () from /usr/lib64/libGLEW.so.2.0
2 0x00007ffff7ba87a3 in glewInit () from /usr/lib64/libGLEW.so.2.0
3 0x0000555555554bd4 in main (argc=1, argv=0x7fffffffdf18) at test.c:39
我在Ubuntu 17.04上,并使用Ubuntus pacakge manager安装glew / glfw
sudo apt-get install libglew2.0 libglew-dev libglfw3 libglfw3-dev
我正在遵循本教程here。如果有什么不同,我没有运行桌面管理器或合成器。我正在使用i3wm。但是,与compton合成器一起运行时,segfault仍然会发生。
最后,为了提供一些额外的信息,
ldd
将test
的链接库显示为linux-vdso.so.1 => (0x00007ffc44ce9000)
libGLEW.so.2.0 => /usr/lib64/libGLEW.so.2.0 (0x00007f8e53dc9000)
libglfw.so.3 => /usr/lib/x86_64-linux-gnu/libglfw.so.3 (0x00007f8e53b86000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8e537bf000)
libGL.so.1 => /usr/lib/nvidia-375/libGL.so.1 (0x00007f8e5351b000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f8e53313000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8e53008000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8e52e04000)
libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f8e52acb000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8e528ad000)
libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f8e526a2000)
libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f8e5249f000)
libXxf86vm.so.1 => /usr/lib/x86_64-linux-gnu/libXxf86vm.so.1 (0x00007f8e52297000)
libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f8e5208d000)
/lib64/ld-linux-x86-64.so.2 (0x000055e0ef28b000)
libGLX.so.0 => /usr/lib/nvidia-375/libGLX.so.0 (0x00007f8e51e5d000)
libGLdispatch.so.0 => /usr/lib/nvidia-375/libGLdispatch.so.0 (0x00007f8e51b8f000)
libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f8e5196d000)
libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f8e5175b000)
libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f8e5154f000)
libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f8e51349000)
libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f8e51145000)
libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f8e50f3f000)
谢谢你的帮助。
最佳答案
There is a bug report表示GLEW中的核心配置文件已被破坏,直到2.0.0。
因此,您应该更新GLEW。如果那不起作用,请通过将GLFW_OPENGL_CORE_PROFILE
标志替换为GLFW_OPENGL_COMPAT_PROFILE
切换到兼容性配置文件。
关于c - glewInit Segfault与GLEW 2.0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44751578/