使用 glew 和 mingw 时未定义引用?

Undefined reference when using glew and mingw?

我正在使用 Eclipse,我最初是从网站上下载二进制文件,直到有人指出我需要从源代码构建它才能使其与 mingw 一起工作,所以我这样做了,我得到了这些文件:glew32.dll, libglew32.a, 和 libglew32.dll.a

我将 glew32.dll 放到了调试文件夹中,并链接了库,但没有成功。

奇怪的部分:GLenum status = glewInit(); 有效,但 glClearColorglClear 无效,我收到 undefined reference to 错误试着打电话给他们。

请查看这些屏幕截图:http://imgur.com/a/L8iNb and http://imgur.com/a/nYoWD

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_ */

要设置 GLEW, a current OpenGL Context is needed (see Creating an OpenGL Context (WGL) 了解更多信息)。

创建 OpenGL 上下文并window

A OpenGL Context and a window can easily created by SDL, GLFW or GLUT (see Initializing GLEW 了解更多信息)。

初始化SDL

如果您使用 SDL,则必须创建 window 并且必须创建 OpenGL 上下文。

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

请注意,您应该使用 SDL_GetError 检查错误。

OpenGL 上下文必须成为当前上下文才能使用它。为此使用 SDL_GL_MakeCurrent

SDL_GL_MakeCurrent( window, glContext );

初始化GLUT

要设置 GLUT 你必须使用 glutInit 并且可以按照初始化 glew 的说明进行操作.

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

初始化GLFW

注意,glfwInit returns GLFW_TRUE 如果成功:

if ( glfwInit() != GLFW_TRUE )
    return;

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

glfwMakeContextCurrent( wnd );

创建 OpenGL Context and you have made it become the current context, you have to initialize glew 之后。

设置GLEW

注意 glewInit returns GLEW_OK 如果成功:

if ( glewInit() != GLEW_OK )
    return;

为了 link 正确地使用 GLEW 库,您必须设置正确的 预处理器定义:

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

另请参阅 GLEW Linker Errors (undefined reference to `__glewBindVertexArray')

的答案

所以基本上要解决这个问题,您需要从 glew 网站下载源代码并自己编译。您使用命令提示符进入您下载的文件夹的目录并按顺序逐行执行这些命令

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

最后:

gcc-ar cr lib/libglew32.a src/glew.o(尽管可能不需要 "gcc-",但它适合我)

完成后,左键单击项目并转到 Properties,然后在 C/C++ Build 转到 settings,然后在 MinGW C++ Linker 下单击 Libraries。一旦你在那里确保你的 Library search path 是正确的(Eclipse 寻找你的库的地方)然后在 Libraries 中输入这些一:glew32 opengl32 glu32 glew32.dll SDL2 SDL2main SDL2_test

此外,当您从源代码编译时,在您的 glew 文件夹中的 lib 文件夹中应该有一个带有 .dll 而不是 .a 扩展名的 glew32从网站下载,将其放入调试中(创建 .exe 的位置)。为 .dllnot.dll.aSDL 做同样的事情,并确保你有你的包含文件夹glewSDLGCC C++ Compiler 下设置(也在你的设置下 C/C++ 生成器)。它现在应该工作了。