New To Open GL and having some trouble

Hello,
I’m encountering an issue when trying to compile my project using Visual Studio. The error is as follows

#error: gl.h included before glew.h

This error occurs when I try to build my project. From my understanding, glew.h needs to be included before gl.h, but I can’t find any instances where I explicitly include gl.h before glew.h.

Steps I have taken so far:

  • Ensured that glew.h is included before any OpenGL-related headers.
  • Checked all files for any accidental #include <GL/gl.h> statements.
  • Ensured glewInit() is called correctly in my project initialization.

Build Information:

  • IDE: Visual Studio 2022
  • Dependencies: GLEW, GLFW
  • Compiler: MSVC 2019 (x64)

What I’ve Tried:

  • Reorganized includes to ensure glew.h always appears before gl.h.
  • Rebuilt the project after each change.
  • Cleaned and rebuilt the solution multiple times.
  • Checked if any third-party libraries are including gl.h incorrectly.

Any help or suggestions would be appreciated. Let me know if I need to provide more information about the issue.

Thanks!

In general one way you can debug these kind of things is by instructing your compiler to write out the preprocessed source, for visual studio see here how to get that.

In this case, based on the information you’ve provided my guess would be that GLFW is including gl.h, so place your glew.h includes before the ones for GLFW - see the GLFW tutorial for how they do this for GLAD (an alternative to GLEW).
When using GLEW you should not need to include gl.h directly at all.

First, the IDE will tell you which source file it was compiling when the error occurred, so you only need to consider that one. Move the #include statement for glew.h up until the error stops occurring.