c - glewInit Segfault與GLEW 2.0



opengl segmentation-fault (1)

我試圖寫一個簡單的程序,只是打開一個窗口與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上的backtrace給出

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

我在這裡按照這個教程。 如果它有什麼不同,我沒有一個桌面管理器或合成器運行。 我正在使用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)

謝謝你的幫助。


有一個錯誤報告說,核心配置文件在GLEW中被打破,直到2.0.0。

因此,你應該更新GLEW。 如果這不起作用,請通過將GLFW_OPENGL_CORE_PROFILE標誌替換為GLFW_OPENGL_CORE_PROFILE來切換到兼容性配置文件。





glew