I made a command line tool using Objective-C and Metal framework. But I want to encapsulate the executable file with the metallib
.
I am wondering if there is a way I can merge these two so that the executable file can run standalone?
You can embed the .metallib file into the executable as a loader section, using the -sectcreate <segname> <sectname> <path>
option. If you're using the compiler to drive linking (which is pretty typical), you would pass that as -Wl,-sectcreate,<segname>,<sectname>,<path>
. You can just pick arbitrary segment and section names of up to 15 characters in length. Avoid names prefixed with underscores as those are reserved to the system.
Then, you can retrieve the data at run-time using getsectdata()
. Construct an NSData
from that and pass it to -[MTLDevice newLibraryWithData:error:]
.