Metal default library not found

When I used metal api to build a thrid static library for producing ".a" lib.

But I add my ***.metal file into my library project target, and run an example project which depend on this metal static lib. it failed at this place:

id <MTLLibrary> _flibrary = [_mDevice newDefaultLibrary];//failed.

erro:/BuildRoot/Library/Caches/com.apple.xbs/Sources/Metal/Metal-56.7/Framework/MTLLibrary.mm:1842: failed assertion `Metal default library not found'

But when I add my ***.metal file into my example project, and it works.

My question is : I do not want to show my ***.metal file for example project, what method could I using for hiding the metal file? Please show me ,thx...

Replies

To create the default

MTLLibrary
, Metal looks for a default.metallib in the main bundle. The problem is that, when building static libraries, the default.metallib has no bundle to be written into, since static libraries don't have bundles, so it's simply written into the same directory as the .a file.


If your primary goal is to avoid sharing your Metal source, and you really want to use a static library (as opposed to, say, a framework), you can compile your Metal source into a .metallib file which you distribute alongside the .a file and add it to the Copy Bundle Resources phase of your app target. The compiled metallib will then reside in the app's main bundle, and you can access it from either the static library or the app with the following code:


NSError *error = nil;
NSString *libPath = [[NSBundle mainBundle] pathForResource:@"MySecretMetalLib" ofType:@"metallib"];
id<MTLLibrary> library = [device newLibraryWithFile:libPath error:&error];


To create the .metallib, you can use the command-line Metal compiler tools included with Xcode (or the new Metal Library target template in Xcode 8), or you can leave the .metal file as a source file in your static library and copy the default.metallib that gets produced when it gets built over to the project you'll be distributing.

Post not yet marked as solved Up vote reply of wcm Down vote reply of wcm

Hello guys. If you are working with the latest Xcode 15 and iOS 17, delete derived data, close Xcode (reboot your updated iPhone) and run the app. You will have to wait a couple of minutes and your app will start working again. Hopping it will work for you too. Best regards. Note: You will get lots of log messages before the screen turns on again.