我必须包括哪些库才能访问GLUT/OpenGL(不带XCode的Mac)?

我必须包括哪些库才能访问GLUT/OpenGL(不带XCode的Mac)?,c,macos,opengl,glut,C,Macos,Opengl,Glut,标题是问题的核心,但问题源于我从SpaceSimulator.net的教程中获得的这段代码。我会把它贴在这里 #include <stdio.h> #include <stdlib.h> #include <GLUT/glut.h> #include <OpenGL/gl.h> #include <OpenGL/glu.h> typedef struct { float x,y,z; } vertex_type; typed

标题是问题的核心,但问题源于我从SpaceSimulator.net的教程中获得的这段代码。我会把它贴在这里

#include <stdio.h>
#include <stdlib.h>
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

typedef struct {
    float x,y,z;
} vertex_type;

typedef struct {
    int a,b,c;
} polygon_type;

#define MAX_POLYGONS 2000
polygon_type polygon[MAX_POLYGONS];

#define MAX_VERTICES 2000
vertex_type vertex[MAX_VERTICES];

typedef struct {
    vertex_type vertex[MAX_VERTICES];
    polygon_type polygon[MAX_POLYGONS];
} obj_type,*obj_type_ptr;

int screen_width, screen_height, filling;
GLfloat rotation_x_increment, rotation_y_increment, rotation_z_increment;
GLfloat rotation_x, rotation_y, rotation_z;

obj_type cube =
{
    {
        -10,-10, 10, //vertex v0
         10,-10, 10, //vertex v1
         10,-10,-10, //vertex v2
        -10,-10,-10, //vertex v3
        -10, 10, 10, //vertex v4
         10, 10, 10, //vertex v5
         10, 10,-10, //vertex v6
        -10, 10,-10  //vertex v7
    },
    {
        0, 1, 4, //polygon v0,v1,v4
        1, 5, 4, //polygon v1,v5,v4
        1, 2, 5, //polygon v1,v2,v5
        2, 6, 5, //polygon v2,v6,v5
        2, 3, 6, //polygon v2,v3,v6
        3, 7, 6, //polygon v3,v7,v6
        3, 0, 7, //polygon v3,v0,v7
        0, 4, 7, //polygon v0,v4,v7
        4, 5, 7, //polygon v4,v5,v7
        5, 6, 7, //polygon v5,v6,v7
        3, 2, 0, //polygon v3,v2,v0
        2, 1, 0, //polygon v2,v1,v0
    }
};


void init(void)
{
   glClearColor(0.0, 0.0, 0.2, 0.0);
   glShadeModel(GL_SMOOTH);
   glViewport(0,0,screen_width,screen_height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f,1000.0f);
   glEnable(GL_DEPTH_TEST);
   glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
}

void resize(int width, int height)
{
   screen_width=width; 
   screen_height=height; 
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glViewport(0,0,screen_width,screen_height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f,1000.0f);
   glutPostRedisplay ();
}

void keyboard_s (int key, int x, int y)
{
   switch (key)
   {
      case GLUT_KEY_UP:
         rotation_x_increment = rotation_x_increment +0.005;
      break;
      case GLUT_KEY_DOWN:
         rotation_x_increment = rotation_x_increment -0.005;
      break;
      case GLUT_KEY_LEFT:
         rotation_y_increment = rotation_y_increment +0.005;
      break;
      case GLUT_KEY_RIGHT:
         rotation_y_increment = rotation_y_increment -0.005;
      break;
   }
}

void keyboard (unsigned char key, int x, int y)
{
   switch (key)
   {
      case ' ':
         rotation_x_increment=0;
         rotation_y_increment=0;
         rotation_z_increment=0;
      break;
      case 'r': case 'R':
         if (filling==0)
         {
            glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
            filling=1;
         } 
         else 
         {
            glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
            filling=0;
         }
      break;
   }
}

void display(void)
{
   int l_index;
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.0,0.0,-50);
   rotation_x = rotation_x + rotation_x_increment;
   rotation_y = rotation_y + rotation_y_increment;
   rotation_z = rotation_z + rotation_z_increment;
   if (rotation_x > 359) rotation_x = 0;
   if (rotation_y > 359) rotation_y = 0;
   if (rotation_z > 359) rotation_z = 0;
   glRotatef(rotation_x,1.0,0.0,0.0);
   glRotatef(rotation_y,0.0,1.0,0.0);
   glRotatef(rotation_z,0.0,0.0,1.0);
   glBegin(GL_TRIANGLES);
   for (l_index=0;l_index<12;l_index++)
   {
      glColor3f(1.0,0.0,0.0);
      glVertex3f( cube.vertex[ cube.polygon[l_index].a ].x, cube.vertex[ cube.polygon[l_index].a ].y, cube.vertex[ cube.polygon[l_index].a ].z);
      glColor3f(0.0,1.0,0.0);
      glVertex3f( cube.vertex[ cube.polygon[l_index].b ].x, cube.vertex[ cube.polygon[l_index].b ].y, cube.vertex[ cube.polygon[l_index].b ].z);
      glColor3f(0.0,0.0,1.0);
      glVertex3f( cube.vertex[ cube.polygon[l_index].c ].x, cube.vertex[ cube.polygon[l_index].c ].y, cube.vertex[ cube.polygon[l_index].c ].z);
   }
   glEnd();
   glFlush();
   glutSwapBuffers();
}

int main(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(screen_width,screen_height);
    glutInitWindowPosition(0,0);
    glutCreateWindow("www.spacesimulator.net - 3d engine tutorials: Tutorial 2");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc (resize);
    glutKeyboardFunc (keyboard);
    glutSpecialFunc (keyboard_s);
    init();
    glutMainLoop();
}
旁白:这是什么阶段的汇编?它是过去的语法错误,但没有完全编译

所以我怀疑我所包含的库是不正确的,或者这些库的Mac版本在某种程度上是不同的;尽管我怀疑后者,因为过剩应该是可移植的

要获得这些GLUT/OpenGL函数,我必须包含哪些库?我知道我必须包括“GLUT/OpenGL的”;我希望有更具体一点的东西


另外,我真的有一个痛处,就是从C/C++转向objective-C和XCode,所以我希望有一个解决方案不涉及这种切换。

将OpenGL框架添加到链接器选项中。GLUT不是OpenGL的一部分,必须单独安装。一旦安装,您也可以添加GLUT框架。

我迟到了,但我想为有同样问题的人节省一些精力。坦率地说,达滕沃尔夫的答案并不明确

OpenGL和GLUT随OS和Xcode安装而来。默认路径是
/System/Library/Frameworks/OpenGL.framework
/System/Library/Frameworks/GLUT.framework

您可以直接包含所需的标题,并使用以下链接器选项在命令行上编译代码:

gcc YOUR_CODE -framework OpenGL -framework GLUT
顺便说一句,在Windows上,“GLUT”是“GL”。如果要避免跨平台问题,可以使用以下预处理器语句:

#if defined(__APPLE__) && defined(__MACH__)
#  include <GLUT/glut.h>
#else 
#  include <GL/glut.h>
#endif
#如果已定义(uuu苹果公司uuu)和已定义(uu马赫uu)
#包括
#否则
#包括
#恩迪夫

这似乎是链接器阶段的错误。Mac OS X和OpenGL都有GLUT。但是,在进一步研究您所说的内容之后,我发现可以通过“-framework OpenGL-framework GLUT”命令行将框架添加到链接器中。最后,我可以在没有XCode的情况下进行开发。如果这听起来像是noob,那么很抱歉。我也面临同样的问题。如何将此标志添加到cmakelist.txt中,以便通过“make”构建它?因为这是最近复制的原始标志:我将使用顺序
-framework GLUT-framework OpenGL
。您通常希望按照从较高级别到较低级别的顺序链接库/框架,以便在链接器处理较低级别库时,可以解析较高级别库中引用的符号。
#if defined(__APPLE__) && defined(__MACH__)
#  include <GLUT/glut.h>
#else 
#  include <GL/glut.h>
#endif