Undefined reference when using glew and mingw?

前端 未结 2 517
灰色年华
灰色年华 2020-12-22 06:50

I am using Eclipse, I had originally downloaded the binary from the website until someone pointed out that I needed to build it from source to make it work with mingw, so I

相关标签:
2条回答
  • 2020-12-22 07:01

    To set up GLEW, a current OpenGL Context is needed (see Creating an OpenGL Context (WGL) for more information).

    Create OpenGL context and window

    A OpenGL Context and a window can easily created by SDL, GLFW or GLUT (see Initializing GLEW for more information).

    Initilize SDL

    If you are using SDL you have to create the window and you have to create the OpenGL context.

    SDL_Window *window = SDL_CreateWindow(""OGL window", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL );
    SDL_GLContext glContext = SDL_GL_CreateContext( window );
    

    Note, you should check for errors with SDL_GetError.

    The OpenGL context has to become the current context before you use it. Use SDL_GL_MakeCurrent therefor.

    SDL_GL_MakeCurrent( window, glContext );
    

    Initilize GLUT

    To set up GLUT you have to use glutInit and can follow the instructions of initializing glew.

    glutInit(&argc, argv);
    glutCreateWindow("OGL window");
    

    Initilize GLFW

    Note, glfwInit returns GLFW_TRUE if succeded:

    if ( glfwInit() != GLFW_TRUE )
        return;
    
    GLFWwindow *wnd = glfwCreateWindow( width, height, "OGL window", nullptr, nullptr );
    if ( wnd == nullptr )
    {
        glfwTerminate();
        return;
    }
    
    glfwMakeContextCurrent( wnd );
    

    After you have created an OpenGL Context and you have made it become the current context, you have to initialize glew.

    Set up GLEW

    Note that glewInit returns GLEW_OK if succeded:

    if ( glewInit() != GLEW_OK )
        return;
    

    To link the GLEW library correctly you have to set up proper preprocessor definitions:

    On Windows, you also need to define the GLEW_STATIC preprocessor token when building a static library or executable, and the GLEW_BUILD preprocessor token when building a dll

    See also the answer to GLEW Linker Errors (undefined reference to `__glewBindVertexArray')

    0 讨论(0)
  • 2020-12-22 07:10

    So basically to solve this problem you want to download the source from the glew website and compiler it yourself. You use the command prompt to get in the directory of the folder you downloaded and execute these commands line by line in order:

    gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c

    gcc -nostdlib -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32

    and finnally:

    gcc-ar cr lib/libglew32.a src/glew.o (though the "gcc-" may not be needed, it was for me)

    Once you're done with that left click on your project and go to Properties, then under C/C++ Build go to settings, then under MinGW C++ Linker click in Libraries. Once you're there make sure your Library search path is correct (the place where Eclipse looks for your libraries) then in Libraries enter these one by one: glew32 opengl32 glu32 glew32.dll SDL2 SDL2main SDL2_test

    Also when you compiled from source there should be a glew32 with a .dll not a .a extension in your lib folder inside the glew folder you downloaded from the website, drop that in into your debug (where your .exe is created). Do the same for the .dllnot the .dll.a for SDL and also make sure you have your include folders for both glew and SDL set up under the GCC C++ Compiler (also under your settings for the C/C++ Builder). It should work now.

    0 讨论(0)
提交回复
热议问题