Hello, I've searched for other posts on this topic but I haven't found anything that provides an answer.
Here's my test program snippet, using the Xcode Mac Application Obj-C starter project:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
char const * dyld_library_path_original = getenv("DYLD_LIBRARY_PATH");
char dyld_library_path_new[1024];
strcpy(dyld_library_path_new, "/Applications/MATLAB_R2020a.app/bin/maci64:");
strcat(dyld_library_path_new, dyld_library_path_original);
int rc = setenv("DYLD_LIBRARY_PATH", dyld_library_path_new, 1);
NSLog(@"DYLD_LIBRARY_PATH=%s, rc=%d", getenv("DYLD_LIBRARY_PATH"), rc);
void * handle1 = dlopen("libeng.dylib", RTLD_NOW);
NSLog(@"Test 1: dlopen(libeng.dylib) = %p, err=%s", handle1, dlerror());
void * handle2 = dlopen("/Applications/MATLAB_R2020a.app/bin/maci64/libeng.dylib", RTLD_NOW);
NSLog(@"Test 2: dlopen(libeng.dylib) = %p, err=%s", handle2, dlerror());
}
As you can see, I'm trying to dlopen a dylib in the installed MATLAB application. But I don't think the specific dylib matters. What matters is that I'm dlopening it using just the leaf name of the path, after setting DYLD_LIBRARY_PATH at runtime to include MATLAB's dir (Test 1). This fails.
But when I dlopen it with the full path (Test 2) it works. Here's the output:
DYLD_LIBRARY_PATH=/Applications/MATLAB_R2020a.app/bin/maci64:/Users/hecht/Library/Developer/Xcode/DerivedData/TestML-droybqyctybedebamivvyiixjhnn/Build/Products/Debug:/usr/lib/system/introspection, rc=0
Test 1: dlopen(libeng.dylib) = 0x0, err=dlopen(libeng.dylib, 2): image not found
Test 2: dlopen(libeng.dylib) = 0x6000039041a0, err=(null)
I have Hardened Runtime enabled, with these entitlements turned on:
- Allow DYLD Environment Variables
- Disable Library Validation
The second one is doing its job, because without it, Test 2 fails also.
But the first one doesn't help in allowing me to modify DYLD_LIBRARY_PATH at runtime. If I set the environment variable before launching (Xcode > Product > Scheme > Edit Scheme > Arguments > Environment Variables) then Test 1 works. Is there a way to get dyld to honor changes to this environment variable made at runtime?