PDA

View Full Version : error C1189: #error : gl.h included before glew.h



mahmoud_arafa
04-12-2017, 09:01 PM
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?

Dark Photon
04-13-2017, 05:41 AM
"error C1189: #error : gl.h included before glew.h".
So how to solve this problem?

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 (https://stackoverflow.com/questions/8978997/how-can-i-see-the-output-of-the-visual-c-preprocessor) 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.

mhagain
04-13-2017, 06:15 AM
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.

mahmoud_arafa
04-13-2017, 03:13 PM
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.

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?

GClements
04-13-2017, 06:55 PM
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.


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

mahmoud_arafa
04-14-2017, 05:53 AM
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".

john_connor
04-15-2017, 12:49 PM
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


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

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)

mahmoud_arafa
04-16-2017, 04:38 AM
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.

Silence
04-16-2017, 05:25 AM
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.

mahmoud_arafa
04-17-2017, 12:59 PM
You can give a link for sure.

Kindly find link for my project below:
https://drive.google.com/open?id=0B9R6IsBsmb7QZ2g4RXVWY1IwNWc

Silence
04-18-2017, 05:58 AM
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...

mahmoud_arafa
04-19-2017, 03:38 PM
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...

Kindly try this link: http://www.mediafire.com/file/vbfz3qraw986d9c/Pacman_AI.rar.

Silence
04-20-2017, 12:13 AM
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

Dark Photon
04-20-2017, 06:08 AM
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\gl ew.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.