Skip to content

Instantly share code, notes, and snippets.

@v3n
Last active November 21, 2023 06:09
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save v3n/27e810ac744b076ceeb7 to your computer and use it in GitHub Desktop.
Save v3n/27e810ac744b076ceeb7 to your computer and use it in GitHub Desktop.
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 http://brew.sh Let's install it. Open you Terminal and paste the following code:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

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 glfw

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

/* Ask for an OpenGL Core Context */
#define GLFW_INCLUDE_GLCOREARB
#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 */
  glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif

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

  /* Make the window's context current */
  glfwMakeContextCurrent(window);

  /* 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 */
    glfwSwapBuffers(window);

    /* Poll for and process events */
    glfwPollEvents();
  }

  glfwTerminate();
  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.

@dineshadepu
Copy link

Hi,
Thanks for the gist. I have a problem compiling my one code of opening a window.
I get the following message

ld: library not found for -lglfw3 clang: error: linker command failed with exit code 1

How can I solve it?

@kuenane
Copy link

kuenane commented Feb 16, 2018

I have the same problem too

@gooderist
Copy link

try replacing -lglfw3 with -lglfw (you can check if the library is actually installed with ls /usr/local/lib)

@qubbit
Copy link

qubbit commented Aug 4, 2018

@gooderist That worked. Thanks!

@kgcrandn
Copy link

Hi,
I am so confuse about the flags of clang++. I can't find "-framework" and "-l***" flags in office document of clang.
So what they mean? and what they do?

@Abbabon
Copy link

Abbabon commented Feb 11, 2020

Thank you so much! Still relevant with the Catalina update.

I also support the -lglfw swap with -lglfw3 !

@v3n
Copy link
Author

v3n commented Feb 11, 2020

Wow, I'm amazed this still works, but glad you found it helpful!

@ritikbhardwaj
Copy link

Thanks man! worked like a charm. I was wondering whether it is still a good practice to use OpenGL on mac platform given that there are other mac specific API's.

@umutbalkan
Copy link

umutbalkan commented Aug 1, 2020

I need to remove all of the glfwWindowHint(...) lines in order to actually render something on canvas. Any idea why this happens? I've tried 4.1 as well nothing changed

All of the tutorials I've found also includes the exact same lines. I'm confused

Update found it:
I specified a very old version of GLSL (1.0) in shader definitions. That was the problem

@quindariuss
Copy link

Worked on M1 Mac on Big Sur!!!

@tergel93
Copy link

thanks.

@quindariuss
Copy link

https://youtu.be/MHlbNbWlrIM I made a video about it!

@bmitc
Copy link

bmitc commented Jan 22, 2022

Thanks for this! I was able to compile and run this on macOS 12.0.1 with no problems and no changes. It also helped me get an example running using Silk.NET's GLFW bindings.

@teacherpan
Copy link

try replacing -lglfw3 with -lglfw (you can check if the library is actually installed with ls /usr/local/lib)

It works. Thank you!

@ComputationTime
Copy link

here's another video that worked for me in VS Code (video has C code, but if you call clang++ instead of clang it works for C++)
https://www.youtube.com/watch?v=6AHq0jTrypw&ab_channel=LuTheCoder

@fonzane
Copy link

fonzane commented Jun 6, 2023

This doesn't work for me. The library is installed to /opt/homebrew/Cellar/glfw.

This solved the issue for me: https://stackoverflow.com/questions/67373307/macos-m1-fatal-error-glfw-glfw3-h-file-not-found

@lukexrout
Copy link

YESSSSSSSSS!!!!!!
thank you kind sir :)

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