0

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 did and I got these files: glew32.dll, libglew32.a, and libglew32.dll.a

I dropped the glew32.dll into the debug folder, and linked the libraries but it did not work.

The weird part: GLenum status = glewInit(); works but glClearColorand glClear do not work and I get a undefined reference to error when I try to call them.

Please see these screenshots: https://i.stack.imgur.com/yfkrG.jpg and https://i.stack.imgur.com/Mg8VR.jpg

C++.cpp

#include <iostream>
#include "classHeaders\display.h"
#include "GL\glew.h"


int main(int argv, char** args){
  display x(800,600,"something");

  while(!x.isClosed()){
    glClearColor(0.0f,0.15f,0.3f,1.0f); //undefined reference to ERROR here
    glClear(GL_COLOR_BUFFER_BIT); //undefined reference to ERROR here
    x.Update();
  }
  return 0;
}

display.cpp

#include "classHeaders\display.h"
#include "GL\glew.h"
#include <iostream>
display::display(int width, int height, const std::string& title){

     SDL_Init(SDL_INIT_EVERYTHING);

     SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
     SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

     m_window = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height, SDL_WINDOW_OPENGL);

     m_glContext = SDL_GL_CreateContext(m_window);

     GLenum status = glewInit(); //NO ERRORS OCCUR

     if(status != GLEW_OK){
         std::cerr << "glew failed to initialize" << std::endl;
     }

     m_isClosed = false;
}

display::~display(){
     SDL_GL_DeleteContext(m_glContext);
     SDL_DestroyWindow(m_window);
     SDL_Quit();
}

bool display::isClosed(){
     return m_isClosed;
}

void display::Update(){
     SDL_GL_SwapWindow(m_window);
     SDL_Event e;

    while(SDL_PollEvent(&e)){
        if(e.type == SDL_QUIT){
           m_isClosed = true;
         }
    }
}

display.h

#ifndef DISPLAY_H_
#define DISPLAY_H_

#include <string>
#include "SDL2\SDL.h"
#undef main /*need to put this in or else it gives me "undefined reference to WinMain" ERROR*/

class display{

    public:
        display(int width, int height, const std::string& title);
        void Update();
        bool isClosed();
        virtual ~display();

    private:
        display(const display& other){}
        display& operator=(const display& other){}
        SDL_Window* m_window;
        SDL_GLContext m_glContext;
        bool m_isClosed;

  };

  #endif /* DISPLAY_H_ */
Larvitar
  • 43
  • 1
  • 6
  • I'm not sure how else to ask this. It's short and to the point. Also if you could help me wirh this problem I would appreciate it. – Larvitar Aug 03 '17 at 05:41
  • It means that when I call them they do not work, I get a "undefined reference to" error which is weird since 'glewInit();' works. Like how is it able to call 'glewInit():' but not 'glClear'? – Larvitar Aug 03 '17 at 05:47
  • Basically it gives me "undefined reference to" errors on the lines where I called the functions. But when I call the 'glewInIt():' function it doesn't give me a "undefined reference to" error. They all the 3 functions mentioned in the post belong to 'glew' so how is it that only 1 works but I get "undefined reference" for the others? – Larvitar Aug 03 '17 at 05:51
  • Are glew libs and your app compiled with the same compiler and same target (32/64 bits)? I recomend not using pre-build glew lib, but either build it on your own or add the source as any other file. – Ripi2 Aug 03 '17 at 11:30

2 Answers2

0

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')

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I tried something similar using SDL2 but I still get the same error, I put the code up there so maybe you can see something I can't? – Larvitar Aug 03 '17 at 07:57
  • Okay I tried the `SDL_GetError` to see if `SDL_Init` worked and it returned a value of 0 so it did. I tried adding `SDL_GL_MakeCurrent` right after the `m_window` and `m_glContext` and it still gave me the **undefined reference to** error to `glClear` and `glClearColor`. Is this just an Issue with my code? All I need to know if it's my code or the way I have everything set up. If it's just my code I can probably figure it out but if there's something wrong with my set up (the `.a` and `.dll`'s and the linker etc...) then I need to figure that out. Though I'm pretty sure it's my code? – Larvitar Aug 03 '17 at 08:50
  • So there's nothing wrong with what I have? Hmm. Like mentioned "glewInit()" works but "glClear" doesn't. SDL2 also works. So I don't think it's my setup? This is really weird – Larvitar Aug 03 '17 at 10:37
  • I added the GLEW_STATIC pre-processor but it still gave the same error. I'm pretty sure it's my code though so I'll look at the links and try to fix it. Thanks for the help. – Larvitar Aug 03 '17 at 11:42
  • okay so apparently it wasn't my code, it had to do with the linker but I finally fixed it. – Larvitar Aug 04 '17 at 07:59
0

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.

Larvitar
  • 43
  • 1
  • 6