10

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

GarrickW
  • 2,181
  • 5
  • 31
  • 38
  • Does that mean you're now #including glew.h twice, once from main.cpp and once from game.h? I guess glew.h isn't written for that (either no guards or #pragma) so you'll either have to add your own guards or take care to include it exactly once. – Rup Dec 20 '11 at 19:17
  • @Rup GLEW has #include guards. It's some other library that is including gl.h. Possibly SFML. – R. Martinho Fernandes Dec 20 '11 at 19:20
  • No, I remove it from main.cpp. I suppose it might be SFML, but I was under the impression that it didn't automatically include gl.h, since there are some tutorials out there for it that want you to explicitly include that file. I will fiddle around with that and see if that solves anything - but I still need to use SFML in main.cpp, at least with my current setup. – GarrickW Dec 20 '11 at 19:22
  • I just tried #including "Game.h" before , such that nothing SFML-related would come before Game.h, but I still get the same error. – GarrickW Dec 20 '11 at 19:25
  • You could adding `#error included here` to the top of gl.h. That should give you an error with the full details of where the inclusion came from. – Rup Dec 20 '11 at 19:30

2 Answers2

19

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.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • I tried moving `#include "Game.h"` up above `#include ` within main.cpp (I shall edit the main post to demonstrate), but that made no difference. I'm not intimately familiar with the way #including things works, so there might be something I'm missing. – GarrickW Dec 20 '11 at 19:28
  • @GarrickW Maybe it's some other header? Can you check the "Build log" tab in Code::Blocks and post the messages that look like "In file included from ..."? That may help find the culprit. – R. Martinho Fernandes Dec 20 '11 at 19:30
  • AH-HA! I've found it - turns out that the problem was in Game.cpp. In there, I had for some reason included `` up above "Game.h". I didn't know the .cpp files for classes were compiled before the headers for the same class, although now that I think of it... I have a lot to learn. Thanks! – GarrickW Dec 20 '11 at 19:31
  • @GarrickW it's a good idea to always include X.hpp first in the X.cpp. Besides not triggering this annoying issue, it also makes sure your .hpp includes everything it needs to work. – R. Martinho Fernandes Dec 20 '11 at 19:33
  • 2
    @GarrickW an #include is replaced by the contents of the included file, so header files are compiled with every file that includes them. – josefx Dec 20 '11 at 20:26
1

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.

Mario
  • 35,726
  • 5
  • 62
  • 78