Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use openGL with gcc on Mac?

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?

like image 615
klm123 Avatar asked Apr 12 '14 14:04

klm123


People also ask

Does OpenGL work on Mac?

OpenGL is available to all Macintosh applications. OpenGL for OS X is implemented as a set of frameworks that contain the OpenGL runtime engine and its drawing software. These frameworks use platform-neutral virtual resources to free your programming as much as possible from the underlying graphics hardware.

How do I run OpenGL in C++?

1.2 Writing Your First OpenGL ProgramCreate a new C++ project: Select "File" menu ⇒ New ⇒ Project... ⇒ C/C++ ⇒ C++ Project ⇒ Next. In "Project name", enter " Hello " ⇒ In "Project type", select "Executable", "Empty Project" ⇒ In "Toolchain", select "Cygwin GCC" or "MinGW GCC" (depending on your setup) ⇒ Next ⇒ Finish.

Does OpenGL work on m1 Mac?

The m1 mac is an awesome machine, but lacks of continuity for OpenGL (especially as OpenGL 4.5 >= introduces a lot of new exciting features that make the code less verbose). As OpenGL 4.1 is still robust and, as Apple did not completely removed OpenGL from the stack, it is still interesting to draw things with OpenGL.


1 Answers

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.

like image 69
datenwolf Avatar answered Sep 20 '22 11:09

datenwolf