How to use openGL with gcc on Mac?

17,228

Solution 1

Your first problem is that Apple's Framework infrastructure actively sabotages portability of code by placing OpenGL headers in a nonstandard path. On Apple platforms the OpenGL headers are located in a directory named OpenGL instead of just GL.

So to portably include the headers you have to write

#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif

Your other problem is, that GLUT is not part of OpenGL and an independent, third party library, that's no longer contained within the OpenGL framework. You need to use the GLUT Framework. And of course that one uses nonstandard include paths as well:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

Compile with the -framework OpenGL -framework GLUT options.

Solution 2

On mavericks using the include

#include <GLUT/glut.h>

works if you compile with the following line

g++ 1.cpp -framework OpenGL -framework GLUT
Share:
17,228
klm123
Author by

klm123

I am physicist. But mostly I do programming. I am interested in C++, parallel and high performance programming.

Updated on June 25, 2022

Comments

  • klm123
    klm123 almost 2 years

    I'm trying to run this openGL example on my Mac from command line using gcc. The XCode is installed, gcc was used many time with other programs (w\o graphics).

    Following to this topic I run:

    g++ 1.cpp -framework OpenGL -lGLU -lglut
    

    and get:

    1.cpp:12:21: fatal error: GL/glut.h: No such file or directory
    

    I found glut.h at /System/Library/Frameworks/GLUT.framework/Headers/ and noted that structure is different (not GL folder). But even removing GL/ and using -I/System/Library/Frameworks/GLUT.framework/Headers/ does not help a lot..

    So I wonder - how to use openGL with gcc on Mac properly?