glfw/glfw

Can't link glfw application on mac

oleksandrh324110 opened this issue · 9 comments

command:

clang src/main.o -o ./bin/main.exe lib/glfw/src/libglfw3.a lib/glad/src/gl.o -lm -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo

error:

Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_CAMetalLayer", referenced from:
       in libglfw3.a[21](cocoa_window.m.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [link] Error 1

main.c is a basic example code from glfw.org

I build glfw with this makefile target:

lib: 
    cd lib/glfw && cmake . && make

versions:

  • macos -> 14.4.1
  • cmake -> 3.29.2
  • make -> 3.81
  • glfw -> 3.4

See:

https://www.glfw.org/docs/latest/build_guide.html#build_link_osx

Also you might find it easier to use this cmake starter to generate your makefile or XCode project:

https://github.com/juliettef/GLFW-CMake-starter

It looks like Apple has changed some code in their Cocoa library so GLFW tries to call wrong function.
Maybe GLFW needs some, hotfix?, for this problem ?

Have you tried cloning the source from git and then using CMake to build GLFW with tests and examples, or alternatively using the GLFW-CMake-starter and following the instructions in the readme to build?

Yes. I added the glfw repo as a submodule and successfully compiled it with cd lib/glfw && cmake . && make
Screenshot 2024-04-26 at 14 31 39

But the problems starts while linking

Also i tried to build some examples and got the same linking error:

Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_CAMetalLayer", referenced from:
       in libglfw3.a[21](cocoa_window.m.o)
ld: symbol(s) not found for architecture arm64

It's something about cocoa_window.m file in glfw

If you have successfully compiled GLFW with cmake and make then you should also have compiled and linked the tests and examples, so the problem is not with linking to GLFW in general but how you are linking to it. So you should see there are test executables built in the tests folder, and examples in the examples folder.

As mentioned you could check the documentation on how to compile and link to GLFW on MacOS, or use the GLFW-CMake-starter. If either of these fail then let us know here along with the details.

Finally fixed it.
As i understood, since the last macos update, developers now also need to link -framework QuartzCore on linking stage,
so we can say that for today, it's not enough to link only these -framework Cocoa -framework OpenGL -framework IOKit

Also note that, i guess, ALL latest versions of macos (for example: BigSur, Monterey, Ventura) need this additional framework while linking (since i downgraded macos from Sonoma to BigSur and the problem still was there:( )

Would be pretty lovely if some contributor updates information about this at https://www.glfw.org/docs/latest/build_guide.html#build_link_osx

I see there's some guy who has already fixed this in documentation coming with sources but not on the website (since last website update was on Fri Feb 23 2024)