Error C1189: #error : gl.h included before glew.h

I have 2 header files: “Display.h” and “Mesh.h”. They both include “GL/glew.h”. I get the following error:
“error C1189: #error : gl.h included before glew.h”.
So how to solve this problem?

[QUOTE=mahmoud_arafa;1286694]“error C1189: #error : gl.h included before glew.h”.
So how to solve this problem?[/QUOTE]

Pretty well just the way the error indicates. Find out where gl.h is being included before one of your glew.h’s, and get rid of it.

If you’re using MSVS, check out this link for how to enable writing of the preprocessor output to a file. There you’ll be able to see what’s included, where, and by whom.

Most likely you have one source file that includes both display.h and mesh.h; as a result this source file includes glew.h twice.

If we look at the contents of glew.h we’ll see the following:

#if defined(__gl_h_) || defined(__GL_H__) || defined(_GL_H) || defined(__X_GL_H)
#error gl.h included before glew.h
#endif

And, a few lines further down:

#define __gl_h_

So what’s happening is that even if you don’t have gl.h included, the second include of glew.h is triggering the include guard and causing the error.

The solution is to only include glew.h once; either via your own include guards or by moving the include of it to elsewhere in your project.

In “Display.i” (preprocessor file of Display source file), I found this:
“gl.h” is included “1 time” in “Display.i”
“glew.h” is included multiple times before “gl.h” and after “gl.h”.

So what’s the next step?

If you’re using glew.h, don’t include gl.h at all, as the two headers will conflict. They both provide the OpenGL API, but differently: gl.h declares function prototypes, glew.h defines macros.

glew.h has a standard include guard around the entire file:


#ifndef __glew_h__
#define __glew_h__
...
#endif /* __glew_h__ */

If you include it more than once, subsequent inclusions will be completely ignored. The #error directives can only be triggered if _gl_h etc are defined but glew_h isn’t (as that will cause the entire header to be skipped).

In “Display.i”, here is the line of including “GL.h”:
" #line 1 “c:\program files\microsoft sdks\windows\v7.1a\include\gl\gl.h” "

I can’t locate the line that includes “GL.h”.

the solution for this very easy: in each .h or .c or .cpp file that needs openGL related stuff, like "GLenum"s or GL types like GLuint, make sure that glew is the first included headder

#include <GL/glew.h>
/* ... then other headders and #defines */

if you create a class (.h and a correspinding .cpp), e.g. “Graphics”, the .h usually contains:

#ifndef GRAPHICS_H
#define GRAPHICS_H

/* ... here some code ... */

#endif // GRAPHICS_H

or:

#pragma once

/* ... here some code ... */

that makes sure that you cant include any headder file multiple times (which usually leads to linking erors, i think).

and if you want to link statically to glew, do everywhere this instead:

#define GLEW_STATIC
#include <GL/glew.h>
/* ... then other headders and #defines */

for example, in the Graphics.h, it’d look like this:

#ifndef GRAPHICS_H
#define GRAPHICS_H

#define GLEW_STATIC
#include <GL/glew.h>

/* ... then other headders and #defines */

#endif // GRAPHICS_H

[QUOTE=mahmoud_arafa;1286710]In “Display.i”, here is the line of including “GL.h”:
" #line 1 “c:\program files\microsoft sdks\windows\v7.1a\include\gl\gl.h” "

I can’t locate the line that includes “GL.h”.[/QUOTE]

sometimes libraries need to include gl.h, for example GLFW. if you do this:

#include <GLFW/glfw3.h>
#include <GL/glew.h>

that will give you the same error as you’ve got (“mhagain” described the reason why this happens above)

I am already using “#pragma once” in all my header files, I made sure that “glew.h” is the first included library in all my header files, yet the problem still exists.

If you would like, I can send you the project link and I will be very grateful to your help.

As john_connor made a reference to it, I would also more go to an inclusion of some other headers which implicitly include gl.h.
If you use glut, glfw, sdl, sfml, glu, glext or anything else, check these. Check your source files too (not only the headers).

You can give a link for sure.

Kindly find link for my project below:

Well, I could navigate threw the file, but it is not possible (at least to me) to open/view any files, nor to download them…
Maybe someone else will have more luck…

[QUOTE=Silence;1286745]Well, I could navigate threw the file, but it is not possible (at least to me) to open/view any files, nor to download them…
Maybe someone else will have more luck…[/QUOTE]

Kindly try this link: Pacman_AI.

Had not tried this since I don’t have visual studio here, but in GameObject.h you include glut.h (which includes gl.h). If, for some reasons, this file is included before the other ones, then you’ll have gl.h included before glew.h

Silence is right.

I pulled it this morning, hacked the ToolsVersion and PlatformToolset so it’d build, and tried a rebuild. Your Initializer.cpp fails to build with:


1>c:\...\pacman_ai\pacman_ai\pacman_ai\include\gl\glew.h(85): fatal error C1189: #error :  gl.h included before glew.h

Initializer.cpp includes Initializer.h, which includes: Pacman.h, Maze.h, Food.h, Monster.h, and Mesh.h.

Pacman.h -> GameObject.h -> GL/glut.h
Mesh.h -> GL/glew.h

So it’s the inclusion of GL/glut.h before GL/glew.h which is pulling in GL/gl.h before GL/glew.h and causing your problem. If you include <GL/glew.h> at the top of Initializer.h, that avoids your problem.

As a better solution, I’d recommend you create a “gl_includes.h” which includes <GL/glew.h> and <GL/glut.h> (in that order), and then you change your .h and .cpp files so that they only pull in “gl_includes.h”. This will ensure you always get these headers in the right order.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.