GLFW: window & contextGLFW 1.0

glfwInit

Starts the GLFW library. Call it once, before any other GLFW function that needs it.

Signature

int glfwInit(void);

How it works

GLFW talks to the operating system for you: it opens windows, creates the OpenGL context and reads the keyboard and mouse. glfwInit connects to the window system (Win32, Cocoa on macOS, X11 or Wayland on Linux) and prepares everything else.

It returns GLFW_TRUE when it worked. If it fails, nothing else in GLFW will work, so stop the program. Calling it again while GLFW is already running does nothing and returns GLFW_TRUE.

Return value

GLFW_TRUE (1) on success, GLFW_FALSE (0) if the window system could not be reached. The reason goes to the error callback (glfwSetErrorCallback) and to glfwGetError.

Tips & pitfalls

  • Set the error callback BEFORE glfwInit, so a failure in glfwInit itself is printed.

  • Call it from the main thread. On macOS, windows and events only work there.

  • Every successful glfwInit needs a matching glfwTerminate at the end of the program.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GLFW_PLATFORM_UNAVAILABLE
No supported window system was found (for example, no display on a Linux server).
GLFW_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1glfwSetErrorCallback(onGlfwError);
2if (!glfwInit()) {
3    std::fprintf(stderr, "GLFW could not start\n");
4    return 1;
5}
6// ... create the window, run the loop ...
7glfwTerminate();
Official GLFW documentation