gl.h included before glew.h

本文探讨了在尝试将OpenGL代码从main函数移至特定类处理时遇到的编译错误。详细介绍了如何调整头文件的包含顺序以避免gl.h在glew.h之前被包含的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

So I'm trying to move my OpenGL code from Main() into a specific class that will handle the 3D graphics only when necessary. Previously, the top of my main.cpp file looked like this:

#define GLEW_STATIC

#include <GL/glew.h>

#include <SFML/Graphics.hpp>

#include <cstdlib>

#include <iostream>

#include <fstream>

#include "Game.h"

This worked well enough. What I tried to do was move all the OpenGL-relevant code into methods of the Game class. So I removed #define GLEW_STATIC and #include <GL/glew.h> from the above, and put them into Game.h, such that the top of Game.h now looks like this:

#define GLEW_STATIC

#include <GL/glew.h>

#include <SFML/Graphics.hpp>

#include <cstdlib>

#include <iostream>

#include <fstream>

#include "Environment.h"

When I try to compile, I get the title error, #error gl.h included before glew.h.

Why is this happening, and how can I use OpenGL code (almost) entirely inside the functions of a specific class without this happening?

EDIT:

I have also tried this configuration in main.cpp, in an attempt to make sure that nothing includes SFML before GLEW.

#include <cstdlib>

#include <iostream>

#include <fstream>

#include "Game.h"

#include <SFML/Graphics.hpp>

Unfortunately, that doesn't help (there's nothing else being included that I'm not mentioning here).

 

[答案]

Some other library is including gl.h. My guess would be SFML. Make sure you include GLEW first in Game.h and check the places where you include Game.h to make sure you're not including SFML or something else that includes gl.h before Game.h.

If you have something like:

#include
					<something_that_includes_gl.h>
					

#include
					"Game.h"
				

It will effectively include gl.h before GLEW.

 

我的情况是在andriod ndk项目中遇到的这个错误,把

#include <GL/glew.h>

这句话放在pch.h的最前面,错误就没有了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值