使用GLEW 2.0 glewInit Segfault

时间:2017-06-25 23:44:41

标签: c opengl segmentation-fault glfw glew

我试图编写一个简单的程序,只打开一个带有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,segfault上的回溯给出了

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管理器安装了glew / glfw

sudo apt-get install libglew2.0 libglew-dev libglfw3 libglfw3-dev

我正在关注本教程here。如果它有所不同,我没有桌面管理器或合成器运行。我正在使用i3wm。但是,当与康普顿合成器一起运行时,仍会发生段错误。

最后,我可以提供一些额外的信息,lddtest的链接库显示为

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)

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

There is a bug report说GLEW的核心配置文件已经破解,直到2.0.0。

因此,您应该更新GLEW。如果这不起作用,请将GLFW_OPENGL_CORE_PROFILE标记替换为GLFW_OPENGL_COMPAT_PROFILE切换到兼容性配置文件。