I have struggled with finding resources for this today and all the ones I saw were tailored to C++ not C. Somehow I have finally found a way to get a window displayed with just C.
The thing that unlocked it for me is that I realised the necessary libraries come with Xcode tools so if you have that installed that is half your problems solved already.
From what I saw on forums online, using GLFW is better than using GLUT for some reason but that's another installation step cuz then you have to use homebrew to install GLFW and use CMAKE to link the library to your code. So in this guide we'd just stick to GLUT also because I don't know jack about this yet, I'm just trying to find my way and I don't really know why GLFW is better than GLUT for myself yet apart from that someone on the internet said so.
This checklist guides you through setting up a basic OpenGL program using Visual Studio Code so you don't pull out your hair. Towards the end I talk about fixes for some errors I face.
Prerequisites
- Xcode: Install from the Mac App Store.
- Command Line Tools: Run xcode-select --install in Terminal.
- Visual Studio Code: Installed with C/C++ extension.
Checklist
Create Project Directory
Open VS Code, create a new folder, and add a file named main.c.
Write Basic OpenGL/GLUT Code
Use correct headers for macOS (, ) and
include a display callback to avoid "no display callback" errors. Just copy this and paste it in your file.
#include <stdio.h>
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
void display(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Hello World");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Compile with Correct Flags
In the terminal, navigate to your project folder and compile with OpenGL and GLUT frameworks for arm64 (Your Mx processor).
If you do not do this it'll use Intel when linking and you'll get an error saying symbol not found for arm64.
gcc -o main main.c -framework OpenGL -framework GLUT -arch arm64
Run the Program
./main
Expect a 500x500 black window. If it closes immediately, ensure glutMainLoop() is included.
Set Up VS Code Build Task (Optional)
I did this because I didn't want to have to type that long code every time I changed something and wanted to compile.
Just create a .vscode folder and then inside a tasks.json file and paste the following code.
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "gcc",
"args": [
"-o",
"main",
"main.c",
"-framework",
"OpenGL",
"-framework",
"GLUT",
"-arch",
"arm64"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
Press Cmd+Shift+B to build.
Errors I faced
"No such file or directory"
Use <OpenGL/gl.h>
, not <GL/gl.h>
.
"Undefined symbols"
Ensure that -framework OpenGL -framework GLUT is in the compile command.
"GLUT Fatal Error: redisplay needed"
Add glutDisplayFunc(display) before glutMainLoop().
Suppress Deprecation Warnings (Optional)
Oh yeah so something is definitely deprecated. I think its OpenGL on Mac but according to my findings it still supports OpenGL 4.1. I just don't think Apple wants to go beyond that or actively even maintain the 4.1 support.
The deprecation warnings were getting annoying so you can shut them up by adding -Wno-deprecated-declarations
to the compile command like so
gcc -o main main.c -framework OpenGL -framework GLUT -arch arm64 -Wno-deprecated-declarations
Or #define GL_SILENCE_DEPRECATION
before headers in main.c file
This setup gets you a working OpenGL environment in C not C++. Happy Hacking! If you run into any problems with this you can drop it in the comments. Feel free to reach out on X @abukaofficial.
Top comments (0)