MetalKit's drawInMTKView function not called if app is built using cmake/make?

I ran into a strange issue testing Metal cpp. The following project is almost the same as Apple's official Metal Cpp example project (https://developer.apple.com/videos/play/wwdc2022/10160/).

https://github.com/shi-yan/testmetal

The apple official example contains an Xcode project, whereas my modified version tries to use cmake.

My cmake project builds and runs, but the window is empty. I debugged it, the root cause is that the drawInMTKView function never gets called.

If I generate an Xcode project using cmake, then build and run my code in Xcode, the resulting app can render to its window just fine.

However, if I run the same app binary through the terminal, it will show an empty window again.

Any idea what might cause this? Thanks.

Accepted Answer

I looked at your CMakeLists.txt file and you are missing MetalKit in your library list. This also affects your Xcode project as well, but you will not notice it until you try to run your binary outside of Xcode. Xcode can determine that MetalKit should be loaded when you debug.

To fix your problem in your CMakeLists.txt, add the line

find_library(MetalKit MetalKit)

and then add ${MetalKit} to your target_link_libraries list.

MetalKit's drawInMTKView function not called if app is built using cmake/make?
 
 
Q