Skip to content

Instantly share code, notes, and snippets.

Forked from v3n/
Created Jun 19, 2018
What would you like to do?
GLFW on OS X starting guide

OpenGL Development on OS X

While it's possible to download packages and install them manually, it's such a hassle. Fortunately for us, OS X has an unofficial package manager called Let's install it. Open you Terminal and paste the following code:

ruby -e "$(curl -fsSL"

Great. Homebrew will automatically install packages to /usr/local. Conveniently, that directory is already in your include and link paths.

Now, some bad news. Freeglut will not run properly on OS X. It attempts to run through the X-Windows server. Unfortunately for us, X11/XQuartz (the X-Windows emulations on OS X) will not support an OpenGL context past OpenGL 1.3. So we will have to use a newer library, called GLFW. Let's install that now.

In Terminal:

brew install glfw3

GLFW3 should now be installed. Here's a substitute tutorial file with GLFW instead of freeglut.

/* Ask for an OpenGL Core Context */
#include <GLFW/glfw3.h>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

int main(int argc, char** argv)
  GLFWwindow* window;

  /* Initialize the library */
  if ( !glfwInit() )
     return -1;

#ifdef __APPLE__
  /* We need to explicitly ask for a 3.2 context on OS X */

  /* Create a windowed mode window and its OpenGL context */
  window = glfwCreateWindow( 1280, 720, "Hello World", NULL, NULL );
  if (!window)
     return -1;

  /* Make the window's context current */

  /* Loop until the user closes the window */
  while (!glfwWindowShouldClose(window))
    /* Render here */
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers

    /* Swap front and back buffers */

    /* Poll for and process events */

  return 0;

Now we need to compile this code. GLFW needs to link against several OS X library. Here's the compiler invokation to compile this code sample properly.

clang++ -std=c++11 -stdlib=libc++ -lglfw3 -framework CoreVideo -framework OpenGL -framework IOKit -framework Cocoa -framework Carbon </path/to/cpp/file>

You can run the resulting file by typing ./a.out

Congratulations! You've completed your first OS X OpenGL project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment