在macOS上编译GLEW和GLUT?

时间:2018-08-19 23:35:21

标签: c++ macos opengl glut glew

我正在尝试将glew和glut标志添加到cmd行中,以正确地编译程序。

没有GLEW和GLUT的代码:

#include <GLFW/glfw3.h>
#include <iostream>
#include <stdio.h>

using namespace std;
int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
    return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
    /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        glEnd();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;

}

使用以下命令运行它:

g++ openGL.cpp -lglfw3 -framework Cocoa -framework OpenGL -framework 
IOKit -framework CoreVideo

工作并绘制一个三角形。添加include glew和glut语句:

#include <GL/glew.h>
#include <GL/glut.h>

,并改用此命令:

g++ openGL.cpp -lglfw3 -framework Cocoa -framework OpenGL -framework 
IOKit -framework CoreVideo -lglut -lGLU -lGL -lGLEW

使编译器放弃20多个错误并放弃。

完整的错误消息:

In file included from openGL.cpp:9:
In file included from ./openGL.hpp:12:
/usr/local/include/GL/glew.h:85:2: error: gl.h included before glew.h
#error gl.h included before glew.h
 ^
/usr/local/include/GL/glew.h:91:2: error: gltypes.h included before glew.h
#error gltypes.h included before glew.h
 ^
/usr/local/include/GL/glew.h:6586:22: error: typedef redefinition with different
      types ('unsigned int' vs 'void *')
typedef unsigned int GLhandleARB;
                     ^
/System/Library/Frameworks/OpenGL.framework/Headers/gltypes.h:28:18: note: 
      previous definition is here
typedef void    *GLhandleARB;
                 ^
In file included from openGL.cpp:9:
In file included from ./openGL.hpp:12:
/usr/local/include/GL/glew.h:19860:17: error: unknown type name
      'PFNGLCOPYTEXSUBIMAGE3DPROC'; did you mean 'PFNGLCOPYTEXSUBIMAGE3DNVPROC'?
GLEW_FUN_EXPORT PFNGLCOPYTEXSUBIMAGE3DPROC __glewCopyTexSubImage3D;
                ^
/usr/local/include/GL/glew.h:16452:28: note: 'PFNGLCOPYTEXSUBIMAGE3DNVPROC'
      declared here
typedef void (GLAPIENTRY * PFNGLCOPYTEXSUBIMAGE3DNVPROC) (GLenum target...
                           ^
/usr/local/include/GL/glew.h:19861:17: error: unknown type name
      'PFNGLDRAWRANGEELEMENTSPROC'; did you mean
      'PFNGLDRAWRANGEELEMENTSEXTPROC'?
GLEW_FUN_EXPORT PFNGLDRAWRANGEELEMENTSPROC __glewDrawRangeElements;
                ^
/usr/local/include/GL/glew.h:10251:28: note: 'PFNGLDRAWRANGEELEMENTSEXTPROC'
      declared here
typedef void (GLAPIENTRY * PFNGLDRAWRANGEELEMENTSEXTPROC) (GLenum mode, ...
                           ^
/usr/local/include/GL/glew.h:19862:17: error: unknown type name
      'PFNGLTEXIMAGE3DPROC'; did you mean 'PFNGLTEXIMAGE3DNVPROC'?
GLEW_FUN_EXPORT PFNGLTEXIMAGE3DPROC __glewTexImage3D;
                ^
/usr/local/include/GL/glew.h:16454:28: note: 'PFNGLTEXIMAGE3DNVPROC' declared
      here
typedef void (GLAPIENTRY * PFNGLTEXIMAGE3DNVPROC) (GLenum target, GLint ...
                           ^
/usr/local/include/GL/glew.h:19863:17: error: unknown type name
      'PFNGLTEXSUBIMAGE3DPROC'; did you mean 'PFNGLTEXSUBIMAGE3DNVPROC'?
GLEW_FUN_EXPORT PFNGLTEXSUBIMAGE3DPROC __glewTexSubImage3D;
                ^
/usr/local/include/GL/glew.h:16455:28: note: 'PFNGLTEXSUBIMAGE3DNVPROC' declared
      here
typedef void (GLAPIENTRY * PFNGLTEXSUBIMAGE3DNVPROC) (GLenum target, GLi...
                           ^
/usr/local/include/GL/glew.h:19865:17: error: unknown type name
      'PFNGLACTIVETEXTUREPROC'; did you mean 'PFNGLACTIVETEXTUREARBPROC'?
GLEW_FUN_EXPORT PFNGLACTIVETEXTUREPROC __glewActiveTexture;
                ^
/usr/local/include/GL/glew.h:5701:28: note: 'PFNGLACTIVETEXTUREARBPROC' declared
      here
typedef void (GLAPIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum texture);
                           ^
/usr/local/include/GL/glew.h:19866:17: error: unknown type name
      'PFNGLCLIENTACTIVETEXTUREPROC'; did you mean
      'PFNGLCLIENTACTIVETEXTUREARBPROC'?
GLEW_FUN_EXPORT PFNGLCLIENTACTIVETEXTUREPROC __glewClientActiveTexture;
                ^
/usr/local/include/GL/glew.h:5702:28: note: 'PFNGLCLIENTACTIVETEXTUREARBPROC'
      declared here
typedef void (GLAPIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum texture);
                           ^
/usr/local/include/GL/glew.h:19867:17: error: unknown type name
      'PFNGLCOMPRESSEDTEXIMAGE1DPROC'
GLEW_FUN_EXPORT PFNGLCOMPRESSEDTEXIMAGE1DPROC __glewCompressedTexImage1D;
                ^
/usr/local/include/GL/glew.h:19868:17: error: unknown type name
      'PFNGLCOMPRESSEDTEXIMAGE2DPROC'
GLEW_FUN_EXPORT PFNGLCOMPRESSEDTEXIMAGE2DPROC __glewCompressedTexImage2D;
                ^
/usr/local/include/GL/glew.h:19869:17: error: unknown type name
      'PFNGLCOMPRESSEDTEXIMAGE3DPROC'; did you mean
      'PFNGLCOMPRESSEDTEXIMAGE3DNVPROC'?
GLEW_FUN_EXPORT PFNGLCOMPRESSEDTEXIMAGE3DPROC __glewCompressedTexImage3D;
                ^
/usr/local/include/GL/glew.h:16450:28: note: 'PFNGLCOMPRESSEDTEXIMAGE3DNVPROC'
      declared here
typedef void (GLAPIENTRY * PFNGLCOMPRESSEDTEXIMAGE3DNVPROC) (GLenum targ...
                           ^
/usr/local/include/GL/glew.h:19870:17: error: unknown type name
      'PFNGLCOMPRESSEDTEXSUBIMAGE1DPROC'
GLEW_FUN_EXPORT PFNGLCOMPRESSEDTEXSUBIMAGE1DPROC __glewCompressedTexSubImage1D;
                ^
/usr/local/include/GL/glew.h:19871:17: error: unknown type name
      'PFNGLCOMPRESSEDTEXSUBIMAGE2DPROC'
GLEW_FUN_EXPORT PFNGLCOMPRESSEDTEXSUBIMAGE2DPROC __glewCompressedTexSubImage2D;
                ^
/usr/local/include/GL/glew.h:19872:17: error: unknown type name
      'PFNGLCOMPRESSEDTEXSUBIMAGE3DPROC'; did you mean
      'PFNGLCOMPRESSEDTEXSUBIMAGE3DNVPROC'?
GLEW_FUN_EXPORT PFNGLCOMPRESSEDTEXSUBIMAGE3DPROC __glewCompressedTexSubImage3D;
                ^
/usr/local/include/GL/glew.h:16451:28: note: 
      'PFNGLCOMPRESSEDTEXSUBIMAGE3DNVPROC' declared here
typedef void (GLAPIENTRY * PFNGLCOMPRESSEDTEXSUBIMAGE3DNVPROC) (GLenum t...
                           ^
/usr/local/include/GL/glew.h:19873:17: error: unknown type name
      'PFNGLGETCOMPRESSEDTEXIMAGEPROC'; did you mean
      'PFNGLGETNCOMPRESSEDTEXIMAGEPROC'?
GLEW_FUN_EXPORT PFNGLGETCOMPRESSEDTEXIMAGEPROC __glewGetCompressedTexImage;
                ^
/usr/local/include/GL/glew.h:2486:28: note: 'PFNGLGETNCOMPRESSEDTEXIMAGEPROC'
      declared here
typedef void (GLAPIENTRY * PFNGLGETNCOMPRESSEDTEXIMAGEPROC) (GLenum targ...
                           ^
/usr/local/include/GL/glew.h:19874:17: error: unknown type name
      'PFNGLLOADTRANSPOSEMATRIXDPROC'; did you mean
      'PFNGLLOADTRANSPOSEMATRIXDARBPROC'?
GLEW_FUN_EXPORT PFNGLLOADTRANSPOSEMATRIXDPROC __glewLoadTransposeMatrixd;
                ^
/usr/local/include/GL/glew.h:7683:28: note: 'PFNGLLOADTRANSPOSEMATRIXDARBPROC'
      declared here
typedef void (GLAPIENTRY * PFNGLLOADTRANSPOSEMATRIXDARBPROC) (GLdouble m[16]);
                           ^
/usr/local/include/GL/glew.h:19875:17: error: unknown type name
      'PFNGLLOADTRANSPOSEMATRIXFPROC'; did you mean
      'PFNGLLOADTRANSPOSEMATRIXFARBPROC'?
GLEW_FUN_EXPORT PFNGLLOADTRANSPOSEMATRIXFPROC __glewLoadTransposeMatrixf;
                ^
/usr/local/include/GL/glew.h:7684:28: note: 'PFNGLLOADTRANSPOSEMATRIXFARBPROC'
      declared here
typedef void (GLAPIENTRY * PFNGLLOADTRANSPOSEMATRIXFARBPROC) (GLfloat m[16]);
                           ^
/usr/local/include/GL/glew.h:19876:17: error: unknown type name
      'PFNGLMULTTRANSPOSEMATRIXDPROC'; did you mean
      'PFNGLMULTTRANSPOSEMATRIXDARBPROC'?
GLEW_FUN_EXPORT PFNGLMULTTRANSPOSEMATRIXDPROC __glewMultTransposeMatrixd;
                ^
/usr/local/include/GL/glew.h:7685:28: note: 'PFNGLMULTTRANSPOSEMATRIXDARBPROC'
      declared here
typedef void (GLAPIENTRY * PFNGLMULTTRANSPOSEMATRIXDARBPROC) (GLdouble m[16]);
                           ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

我正在尝试学习OpenGL,但不知道该怎么做。

2 个答案:

答案 0 :(得分:2)

Glut和GLFW是完全不同的库。参见此处What is the difference between FreeGLUT vs GLFW?

我不建议尝试将它们混合在同一程序中,并且.h文件可能会冲突-这可能解释了许多错误。

通常您可以使用GLFW或GLUT,但不能同时使用它们,而且它们的行为也大不相同。

我还注意到您正在使用即时模式在OpenGL中进行绘制。这很容易,并不是一个不好的起点,但是现在不再像现在这样。因此,为使用顶点阵列对象(VBO)绘制更复杂但速度更快且功能更强大的方法做好准备。在执行即时模式时,请尝试先阅读并做一些VBO教程。

答案 1 :(得分:0)

在GLFW之前必须包含GLEW。
您需要将GLFW和GLEW的包含顺序更改为:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

错误中指出了这种罪恶:

/usr/local/include/GL/glew.h:85:2: error: gl.h included before glew.h