xcode Mac 上的 OpenGL C++ 构建失败,找不到“GL/glut.h”文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27952029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:29:06  来源:igfitidea点击:

OpenGL C++ on Mac build fail 'GL/glut.h' file not found

c++xcodemacosopenglglut

提问by Beast_Code

Attempting to run a sample OpenGL C++ program on OSX Yosemite with xCode.

尝试使用 xCode 在 OSX Yosemite 上运行示例 OpenGL C++ 程序。

When running the program I get 'GL/glut.h' file not found screenshot:

运行程序时,我得到 'GL/glut.h' file not found 屏幕截图:

'GL/glut.h' file not found

找不到“GL/glut.h”文件

However, I do have the glut.h header in the project:

但是,我在项目中有 glut.h 头文件:

glut.h header in the project

项目中的 glut.h 头文件

On a separate forum I read that it should be 'GLUT/glut.h' so I did, but got the following message:

在一个单独的论坛上,我读到它应该是 'GLUT/glut.h' 所以我做了,但得到了以下消息:

GLUT/glut.h

GLUT/glut.h

What do I need to do to have OpenGL with C++ configured?

我需要做什么才能配置带有 C++ 的 OpenGL?

This is the code I am trying to run. If I can run this, then I should have everything ready to go:

这是我试图运行的代码。如果我可以运行它,那么我应该准备好一切:

#include <GLUT/glut.h>
#include <math.h>
//#include <stdlib.h>

const double TWO_PI = 6.2831853;

/*  Initial display-window size.  */
GLsizei winWidth = 400, winHeight = 400;
GLuint regHex;

class screenPt
{
private:
    GLint x, y;

public:
    /*  Default Constructor: initializes coordinate position to (0, 0).  */
    screenPt ( )  {
        x = y = 0;
    }

    void setCoords (GLint xCoord, GLint yCoord)  {
        x = xCoord;
        y = yCoord;
    }

    GLint getx ( ) const  {
        return x;
    }

    GLint gety ( ) const  {
        return y;
    }
};

static void init (void)
{
    screenPt hexVertex, circCtr;
    GLdouble theta;
    GLint k;

    /*  Set circle center coordinates.  */
    circCtr.setCoords (winWidth / 2, winHeight / 2);

    glClearColor (1.0, 1.0, 1.0, 0.0);   //  Display-window color = white.

    /*  Set up a display list for a red regular hexagon.
     *  Vertices for the hexagon are six equally spaced
     *  points around the circumference of a circle.
     */
    regHex = glGenLists (1);   //  Get an identifier for the display list.
    glNewList (regHex, GL_COMPILE);
    glColor3f (1.0, 0.0, 0.0);   //  Set fill color for hexagon to red.
    glBegin (GL_POLYGON);
    for (k = 0; k < 6; k++) {
        theta = TWO_PI * k / 6.0;
        hexVertex.setCoords (circCtr.getx ( ) + 150 * cos (theta),
                             circCtr.gety ( ) + 150 * sin (theta));
        glVertex2i (hexVertex.getx ( ), hexVertex.gety ( ));
    }
    glEnd ( );
    glEndList ( );
}

void regHexagon (void)
{
    glClear (GL_COLOR_BUFFER_BIT);

    glCallList (regHex);

    glFlush ( );
}

void winReshapeFcn (int newWidth, int newHeight)
{
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ( );
    gluOrtho2D (0.0, (GLdouble) newWidth, 0.0, (GLdouble) newHeight);

    glClear (GL_COLOR_BUFFER_BIT);
}

void main (int argc, char** argv)
{
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition (100, 100);
    glutInitWindowSize (winWidth, winHeight);
    glutCreateWindow ("Reshape-Function & Display-List Example");

    init ( );
    glutDisplayFunc (regHexagon);
    glutReshapeFunc (winReshapeFcn);

    glutMainLoop ( );
}

回答by Toby Cronin

When you changed it to 'GLUT/glut.h` it finds the header correctly. Unfortunately now you get all the warning messages about deprecated functions.

当您将其更改为 'GLUT/glut.h` 时,它会正确找到标题。不幸的是,现在您会收到有关已弃用函数的所有警告消息。

For a fix for the deprecated function messages see xcode 5 deprecation warning about glut functionsor Glut deprecation in Mac OSX 10.9, IDE: QT Creator

有关已弃用的函数消息的修复,请参阅有关Mac OSX 10.9 中的glut 函数Glut 弃用的xcode 5 弃用警告,IDE:QT Creator

You also have an error message saying that your main function needs to return an int. This is what will prevent your code from compiling, not the warnings.

您还会收到一条错误消息,指出您的主函数需要返回一个 int。这将阻止您的代码编译,而不是警告。

EditYou should change your main function to have a return type of int.

编辑您应该更改您的主函数,使其返回类型为 int。

int main(int argc, char *argv[])
{
  /* code */
  return 0;
}

Some compilers will accept void as a return type for main but this is a non-standard extension.

一些编译器会接受 void 作为 main 的返回类型,但这是一个非标准的扩展。

回答by user2724262

Try #include <GLUT/glut.h>as the framework linked name is GLUT.

尝试,#include <GLUT/glut.h>因为框架链接名称是 GLUT。