Setup OpenGL on MacOS with VS Code

Tutorial

Selam G.
3 min readSep 18, 2023

Whenever I get annoyed enough that the exact google search term I use for a dumb software setup issue has not been SEO’d by some blog, I take advantage of this by writing up my own blog post.

I needed to compile OpenGL for a class. The professor directed everyone to use XCode IDE, but gave students the freedom to choose their editor of choice. So I had been using VS Code as I like how it’s closer to terminal execution (forces you to really understand what’s going on under the hood).

Of course, the tradeoff is that VS Code is a lot less “automatic”. Problems I at first thought were VS Code problems turned out to really be an issue of malformed clang++ commands. So I dug through tutorials and stackoverflow etc. until I figured that out.

I referenced this other medium post for setting up C++ on Windows a bit as well as this Youtube Video and otherwise dug through information on properly forming clang++ commands.

Setup Steps

  1. Download GLFW and GLAD. These are a windowing system and a function pointer handler respectively, you can read more about why you need each in your own research. (There’s a tutorial website called Learn OpenGL that may be useful)
  2. When you install Glad from here, choose the latest version under API/gl, set the profile to core and hit “generate”. Leave everything else as “None”.

3. From the page that appears after generate, download the glad.zip file and unpack.

4. Rearrange the files into your preferred directory structure. Here’s what mine ended up looking like. Ignore main.exe.dYSM (these are hidden files generated by macOS)

5. In tasks. json, modify your args command like so:

            "args": [
"-std=c++17",
"-fdiagnostics-color=always",
"-Wall",
"-g",
"-I${workspaceFolder}/include",
"-L${workspaceFolder}/lib",
"${workspaceFolder}/src/glad.c",
"${workspaceFolder}/src/*.cpp",
"${workspaceFolder}/lib/libglfw.3.dylib",
"-o",
"${workspaceFolder}/main.exe",
"-framework",
"OpenGL"
],

I learned that you have to move the reference to the glfw library (in this case libglfw.3.dylib ) to the end of the args for it to be properly referenced (as of right now could not tell you why that’s important but it fixed the clang errors I got).

The important thing to note here is that glad.cneeds to be compiled alongside main.cpp

My test version of main.cpp was shamelessly copied from this medium post, here is the exact github link. It’s a test written to show a blank window as below, so if you see this after compiling everything should be working correctly.

successful execution

--

--