Reputation: 3352
I'm building a project with opengl using glfw3. The program does compile and it does run with no errors. However I get compiler warnings saying that functions such as glGenBuffers
, glBindBuffer
, glBufferData
, and glEnableVertexAttribArray
have not been declared. Looking into it these functions are all declared in <GL/glext.h>
(of which is included in gl.h)
Functions that are actually decalared in <GL/gl.h>
are compiling with out warning such as glEnable
, glClear
and glClearColor
.
Compiling (produces warnings):
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o main.o
Linking (no warnings):
gcc -o test main.o `pkg-config --static --libs glfw3`
Upvotes: 3
Views: 3667
Reputation: 213758
You need an OpenGL loader, like glLoadGen.
Most platforms only support an older version of OpenGL with the <GL/gl.h>
header. On Windows you get OpenGL 1.1, with Linux/Mesa you get OpenGL 1.3. This is due to the way dynamic linking works on these platforms. If you link with a function from a newer version of OpenGL but run your program on a system with an older version of OpenGL, your program will fail to run at all. This is solved by forcing people to load OpenGL functions dynamically. You can do this yourself using wglGetProcAddress
, glXGetProcAddress
, or other functions, and glext.h
is designed to help you do this, but it's easier to just use an OpenGL loader.
GLEW is probably the easiest choice, since it's available through most package managers, but it doesn't work correctly in core contexts.
(Note that if you are only targeting macOS / iOS, you can use weak linking instead, and an OpenGL loader is not necessary.)
In addition, I would recommend compiling with -Werror
, or at least -Werror-implicit-function-declaration
, so you can avoid getting implicit declarations by accident.
Upvotes: 3