I have started learning engine development and have decided to use GLAD and SDL2 for my project. I have followed various tutorials and have successfully linked both SDL2 and GLAD to my project. But I have encountered a very strange problem.
I tried to use the glGenBuffers()
function but Visual Studio told me glGenBuffers is undefined
. So I went into the glad.h and searched for glGenBuffers
and found it inside of #ifndef GL_VERSION_1_5
. (I could not use any other functions as well.)
So I tried to comment out the #ifndef
and #endif
for that part and now it worked! I could use glGenBuffers
. And beacuse I know I have version 4.6 of opengl I don't have to worry about the versions.
The problem here is that this feels like I am not doing it like it's supposed to, and that this might introduce problems in the future. So what I did was I put the #ifndef
and #endif
back into it's place. But now the glad.h was absolutely littered with multiple errors of these types:
this declaration has no storage class or type specifier
the #if for this directive is missing
the #endif for this directive is missing
The document was exactly the same as when I got it, but what was even more strange was that now every other function worked as well. Even those not in the #ifndef GL_VERSION_1_5
but for example #ifndef GL_VERSION_3_0
.
And the code in the main.cpp still compiled and ran fine. When I collapse the #ifndef GL_VERSION_1_5
it now says "Active Preprocessor Block" as well, wheras the others say "Inactive Preprocessor Block".
I tried replacing the document with another copy and now I am back to the old problem of it stating glGenBuffers is undefined
. It's worth noting gladLoadGLLoader(( GLADloadproc )SDL_GL_GetProcAddress)
works fine at runtime so it's not a problem with GLAD itself.
Anyone know what might be causing this and how I should go about solving this problem?
The includes I'm currently using:
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <glad/glad.h>
Compile log:
Build started...
1>------ Build started: Project: GrubEngineC++, Configuration: Debug x64 ------
1>GrubEngineC++.cpp
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\GrubEngineC++\include\glad\glad.h(873,1): warning C4005: 'GL_INVALID_INDEX': macro redefinition
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\SDL2\include\SDL_opengl_glext.h(1651): message : see previous definition of 'GL_INVALID_INDEX'
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\GrubEngineC++\include\glad\glad.h(915,1): warning C4005: 'GL_TIMEOUT_IGNORED': macro redefinition
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\SDL2\include\SDL_opengl_glext.h(1726): message : see previous definition of 'GL_TIMEOUT_IGNORED'
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\GrubEngineC++\GrubEngineC++.cpp(15,5): error C3861: 'glGenBuffers': identifier not found
1>Done building project "GrubEngineC++.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The problem is the include order for the OpenGL definitions. As can be seen in the compile log, there are also other definitions like GL_INVALID_INDEX
that are already defined when glad is included.
The SDL_opengl.h
is the OpenGL header for SDL and already contains the definitions for a lot of the OpenGL related symbols and conflicts with the use of glad.h
. You either have to include it after glad.h
or not all (which is better, unless you need methods from SDL_opengl.h
.