Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#error gl.h included before glew.h

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).

like image 608
GarrickW Avatar asked Dec 20 '11 19:12

GarrickW


2 Answers

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.

like image 156
R. Martinho Fernandes Avatar answered Sep 21 '22 01:09

R. Martinho Fernandes


I think I had this issue once, too. It's somehow caused by the way SFML (1.6?) includes the OpenGL stuff.

IIRC (been some time and I don't need GLEW anymore since switching to SFML2) it's due to SFML's Graphics.hpp including GLEW.h, too. Shouldn't happen due to include guards, but I think with some versions this might still happen. It might be possible for you to skip GLEW's header completely, as it's included through SFML anyway.

Which version of SFML are you running? 1.6, 2.0 or the 2.0 with the new API? Also, what's the reason for using GLEW? Something you're missing from SFML? Maybe it's something included in the latest version, so keeps you from having to include it too.

like image 27
Mario Avatar answered Sep 20 '22 01:09

Mario