Switching to glad.h causes crash

Alec Jacobson

November 05, 2018

weblog/

I had a simple OpenGL/glfw program that I'd written on Mac OS X. As such I was including OpenGL via:

#include <OpenGL/gl3.h>

It's been a consistent frustration that on Mac the header include is different from Linux/Windows. Similarly, you never really know what you're getting when you include and link to OpenGL. Thankfully, the glad project can auto-generate a small library that ensures you get the OpenGL you asked for. Then you can change your gl include to

#include <glad/glad.h>

I did this an immediately my program crashed on the first OpenGL call (in my case glGetString). My program wasn't crashing before. I thought it might be the OpenGl version I'd asked for using glad, but that didn't seem to matter.

In the end, I realized that I needed to add a glad initializing before any OpenGL calls. Adding this at the top of my program fixed everything:

if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
{
  std::cerr<<"Failed to load OpenGL and its extensions"<<std::endl;
  return EXIT_FAILURE;
}