I got SDL2 on mac via brew's brew install sdl2
. Now I try to compile a simple C program and I'm unable to start it.
I'm trying to compile as follows:
gcc -Wall -std=c99 -I/opt/homebrew/Cellar/sdl2/2.28.2/include/SDL2 -c ../src/main.c -o target/o/main.o
gcc -Wall -std=c99 -I/opt/homebrew/Cellar/sdl2/2.28.2/include/SDL2 -c ../src/glad/glad.c -o target/o/glad.o
gcc -ldl -L/opt/homebrew/Cellar/sdl2/2.28.2/lib -lSDL2 -o target/csdldemo target/o/main.o target/o/glad.o
Here /opt/homebrew/Cellar/sdl2/2.28.2/include/SDL2
is a path to SDL2 installation.
Application compiles without any issues. However when I start it via ./target/csdldemo
it immediately gets killed by an OS.
I.e.:
./target/csdldemo
zsh: killed ./target/csdldemo
I can still start an application via lldb ./target/csdldemo
and then running it (i.e. via r
command in the lldb prompt), but it fails later when I try to use OpenGL 3.3 functions.
I tried to run it at least using lldb but I think the following doesn't work:
// I run the following after SDL_Init(SDL_INIT_VIDEO) but before SDL_CreateWindow:
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
Also tried setting major/minor version as 3.3
and 4.5
to no avail.
The code that doesn't work on Mac OS X is an attempt to compile shader.
Shader definition:
#version 330 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0, 0.5, 0.2, 1.0);
}
Code that compiles the above:
CHECK(fragmentShader = glCreateShader(GL_FRAGMENT_SHADER));
CHECK(glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL));
CHECK(glCompileShader(fragmentShader));
Here: a CHECK
is a simple macro that checks OpenGL error after executing an OpenGL API call and the error stating version '330' is not supported
shows up after executing glCompileShader
:
ERROR: 0:1: '' : version '330' is not supported
ERROR: 0:1: '' : syntax error: #version
In the above snippet I use glCompileShader
from glad
which was generated as follows:
OpenGL loader generated by glad 0.1.34 on Thu Aug 24 17:59:33 2023.
Language/Generator: C/C++
Specification: gl
APIs: gl=3.0
Profile: compatibility
Extensions:
Loader: True
Local files: True
Omit khrplatform: False
Reproducible: False
Commandline:
--profile="compatibility" --api="gl=3.0" --generator="c" --spec="gl" --local-files --extensions=""
Mac OS X version is 13.4
(Ventura).
Exact same program compiles and works fine on linux.
I suspect that the above is a combination of two issues:
- Inability to run compiled binary on Mac as evidenced by immediate program termination. Unlike others this attempt doesn't show up in security settings so I'm not sure how to make Mac OS to permit execution of my own compiled binaries. Presumably
brew
itself doesn't have this issue as I can use binaries installed viabrew
. - Various sources state that my version of Mac should support OpenGL 4.1, I can only speculate that security subsystem somehow interferes with shader compilation?
Tried:
- Compiling and running the same program on Linux. Tried using cmake for both Mac and Linux as well as using custom Makefile on Mac OS. Everything works as expected on Linux, in all cases I see binary immediately killed on Mac. Like I said, I can still run the program via
lldb
. - Tried setting other versions of OpenGL via corresponding
SDL_GL_SetAttribute
. Tried: 3.3, 4.1 and 4.5.
I wonder what is missing in my setup what makes glCompilerShader to fail. Any steps to diagnose/troubleshoot the problem further would be appreciated.