objective-c call to MTLCreateSystemDefaultDevice() returns null, swift call does not: why?

The following code:-

#import <Metal/Metal.h>
#import <simd/simd.h>
#import <MetalKit/MetalKit.h>

#import <Foundation/Foundation.h>

int main(int argc, char** argv)
{
	id <MTLDevice> device;

	device = MTLCreateSystemDefaultDevice();
	if (!device) {
		printf("no default device\n");
	} else {
		printf("default device: %s\n", [[device name] cStringUsingEncoding:NSISOLatin1StringEncoding]);
	}

	NSArray *devices = MTLCopyAllDevices();
	for (device in devices) {
		NSString *s = [device name];
		printf("all devices: %s\n", [s cStringUsingEncoding:NSISOLatin1StringEncoding]);
	}
}
clang -framework Metal -framework Foundation objc.m -o objc
./objc # gives

no default device
all devices: AMD Radeon Pro 5500 XT

The equivalent swift

cat mtl.sw

#!/usr/bin/swift 

import Foundation
import Cocoa // AppKit, CoreData, Foundation

let device = MTLCreateSystemDefaultDevice()!
let name  = device.name;
print("Default: name: " + name);
./mtl.sw # gives
Default: name: AMD Radeon Pro 5500 XT

My question is why does objective see return null from the call to MTLCreateSystemDefaultDevice() but Swift does not.

I am running this on an iMac 27-inch 2020, i7 model.

The documentation says:

In macOS, in order for the system to provide a default Metal device object, you must link to the Core Graphics framework. You usually need to do this explicitly if you’re writing apps that don’t use graphics by default, such as command line tools.

Thankyou.

Linking against CoreGraphics causes a device to be found.

clang -framework CoreGraphics -framework Metal -framework Foundation objc.m -o objc
./objc 
default device: AMD Radeon Pro 5500 XT
all devices: AMD Radeon Pro 5500 XT
objective-c call to MTLCreateSystemDefaultDevice() returns null, swift call does not: why?
 
 
Q