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:
brew
itself doesn't have this issue as I can use binaries installed via brew
.Tried:
lldb
.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.
For those hitting the same issue: the missing part was SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)
before setting all other attributes.
Thanks to user Ripi2 who suggested this.
As for zsh killing the process - or, more precisely, OS sending SIGKILL to the process - that was due to the absence of code signature. This problem could be diagnosed by looking at crash reports available in the Mac OS X Console application.