Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL2, Mac OS X, and OpenGL: How to avoid including both gl.h and gl3.h?

I'm working on an SDL2 application using the OpenGL 3.2 core profile. When I compile, I get the following warning:

/System/Library/Frameworks/OpenGL.framework/Headers/gl.h:5:2: warning: #warning gl.h and gl3.h are both included. Compiler will not invoke errors if using removed OpenGL functionality. [-Wcpp]

I have to assume that SDL is including gl.h somewhere, because my only includes are as follows:

#define GL3_PROTOTYPES

#include <OpenGL/gl3.h>
#include <SDL2/SDL.h>

While I can simply ignore this, it has already led to one hard-to-find bug when I accidentally used an enum value not available in the core profile. Is there some way to prevent gl.h from being included?

like image 357
Karl Avatar asked Sep 28 '13 22:09

Karl


2 Answers

Take a look at /System/Library/Frameworks/OpenGL.framework/Headers/gl.h, you should see a pretty standard include guard on the first line: #ifndef __gl_h_. If you look at gl3.h you will notice an equally standard include guard for __gl3_h_. This warning is only triggered when both are defined and GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED is undefined.

The easiest way to prevent including anything from within gl.h would be to #define __gl_h_ before it is ever included. To avoid messing up your actual code with something nasty like:

#ifdef __APPLE__
# define __gl_h_
# define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
#endif

I would suggest adding -D__gl_h_ -DGL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED as a compiler switch to your Makefile when you target OS X (so that no matter where gl.h is indirectly included from, it never does anything). This really is not the right way to go about tackling this, and that warning was generated for a reason (which you pointed out in your question -- deprecated OpenGL tokens will not generate compiler warnings / errors when both headers are included).

I think SDL2 itself ought to have some pre-processor mechanism to do what I did above with #ifdef __APPLE__ but in lieu of that, this will get the job done. It is not an error by any means to include both, OS X just provides a handy mechanism to generate compiler errors when deprecated tokens (e.g. GL_MODELVIEW) are used in a project that is supposed to be core 3+.

On other platforms whether you are using core OpenGL 3+ is not so black and white at compile-time, so the compiler cannot be used for this purpose. It is one of those things that Apple does just because they can; "think different."

like image 174
Andon M. Coleman Avatar answered Nov 12 '22 15:11

Andon M. Coleman


This warning goes away in the 2.0.4 release candidate of SDL2. Temp link to release candidate was shared on SDL mailing list. http://forums.libsdl.org/viewtopic.php?t=11305

like image 26
Bram Avatar answered Nov 12 '22 15:11

Bram