GLFW: window & contextGLFW 3.0

glfwSetErrorCallback

Registers a function that GLFW calls with a code and a readable message whenever something fails.

Signature

GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun callback);

How it works

Most GLFW functions do not return an error code. When one fails, GLFW calls your error callback instead. Without one, failures are silent: the window simply does not appear and you do not know why.

The callback receives int error_code (a constant such as GLFW_VERSION_UNAVAILABLE) and const char* description (an English sentence). This is one of the few functions you may call BEFORE glfwInit, and you should.

Parameters

callbackGLFWerrorfun

Your function, void f(int error_code, const char* description), or NULL to remove the current one.

Common values

GLFW_VERSION_UNAVAILABLE
The driver cannot create the OpenGL version you asked for with glfwWindowHint.
GLFW_API_UNAVAILABLE
No OpenGL driver at all (common in virtual machines and remote desktops).
GLFW_NO_CURRENT_CONTEXT
A context function was called with no context current on this thread.
GLFW_FEATURE_UNAVAILABLE
The platform cannot do this at all, e.g. moving a window on Wayland (GLFW 3.4).
GLFW_PLATFORM_ERROR
The operating system refused; the message says why.

Return value

The callback that was set before, or NULL.

Tips & pitfalls

  • The description string belongs to GLFW and is only valid inside the callback. Copy it if you need it later.

Example

example.cpp
1void onGlfwError(int code, const char* description) {
2    std::fprintf(stderr, "GLFW error 0x%X: %s\n", code, description);
3}
4
5int main() {
6    glfwSetErrorCallback(onGlfwError);   // before glfwInit
7    if (!glfwInit()) return 1;
8}
Official GLFW documentation