简体   繁体   English

SDL2,Mac OS X和OpenGL:如何避免同时包含gl.h和gl3.h?

[英]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. 我正在使用OpenGL 3.2核心配置文件处理SDL2应用程序。 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. /System/Library/Frameworks/OpenGL.framework/Headers/gl.h:5:2:警告:#warning gl.h和gl3.h都包括在内。 Compiler will not invoke errors if using removed OpenGL functionality. 如果使用已删除的OpenGL功能,编译器将不会调用错误。 [-Wcpp] [-Wcpp]

I have to assume that SDL is including gl.h somewhere, because my only includes are as follows: 我必须假设SDL在某处包含gl.h,因为我的唯一包括如下:

#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? 有没有办法阻止gl.h被包括在内?

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_ . 看一下/System/Library/Frameworks/OpenGL.framework/Headers/gl.h ,你应该在第一行看到一个非常标准的包含守卫: #ifndef __gl_h_ If you look at gl3.h you will notice an equally standard include guard for __gl3_h_ . 如果你看一下gl3.h你会发现一个同样的标准包括后卫__gl3_h_ This warning is only triggered when both are defined and GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED is undefined. 仅在定义了两个并且未定义GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED时才会触发此警告。

The easiest way to prevent including anything from within gl.h would be to #define __gl_h_ before it is ever included. 防止在gl.h包含任何内容的最简单方法是在包含之前#define __gl_h_ 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). 当你定位OS X时,我建议将-D__gl_h_ -DGL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED作为编译器开关添加到Makefile中(这样无论gl.h是间接包含在哪里,它都不会做任何事情)。 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). 这实际上不是解决这个问题的正确方法,并且出于某种原因生成警告(您在问题中指出了这一点) - 当包含两个标头时,不推荐使用的OpenGL令牌不会生成编译器警告/错误。

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. 我认为SDL2本身应该有一些预处理器机制来完成我上面用#ifdef __APPLE__所做的事情,但代替它,这将完成工作。 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 (eg GL_MODELVIEW ) are used in a project that is supposed to be core 3+. 通过任何方式包含两者都不是错误,OS X只是提供了一个方便的机制来生成编译器错误,当不推荐使用的令牌(例如GL_MODELVIEW )用于应该是核心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. 在其他平台上,无论您使用的是核心OpenGL 3+在编译时都不是那么黑白,因此编译器不能用于此目的。 It is one of those things that Apple does just because they can; 这是苹果公司所做的事情之一,因为它们可以; "think different." “想想不同。”

This warning goes away in the 2.0.4 release candidate of SDL2. 这个警告在SDL2的2.0.4版本候选版本中消失了。 Temp link to release candidate was shared on SDL mailing list. 在SDL邮件列表上共享了发布候选版本的临时链接。 http://forums.libsdl.org/viewtopic.php?t=11305 http://forums.libsdl.org/viewtopic.php?t=11305

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM