@@ -169,7 +169,7 @@ static bool FindInStandardPaths(const char* library_name, char* resolved_path) {
169169#if defined(__ANDROID__ )
170170 const char * standard_paths [] = {"/system/lib64" , "/system/lib" , "/system/vendor/lib64" , "/system/vendor/lib" , NULL };
171171#elif defined(__APPLE__ )
172- const char * standard_paths [] = {"/usr/lib" , "/lib" , "/usr/local/lib" , "/opt/local/lib" , "/opt/homebrew/lib" , NULL };
172+ const char * standard_paths [] = {"/usr/lib" , "/usr/lib/system" , "/ lib" , "/usr/local/lib" , "/opt/local/lib" , "/opt/homebrew/lib" , NULL };
173173#else
174174 const char * standard_paths [] = {"/usr/lib" , "/lib" , "/usr/local/lib" , NULL };
175175#endif
@@ -378,6 +378,36 @@ static bool FindInLdSoConfFile(const char* conf_file, const char* library_name,
378378}
379379#endif // !_WIN32
380380
381+ #ifdef _WIN32
382+ #include <windows.h>
383+ #else
384+ #include <dlfcn.h>
385+ #endif
386+
387+ static bool JustTryDlOpenOnBasename (const char * library_name , char * resolved_path ) {
388+ // In case our attempts to find the library fail, we can try to load it directly. Sometimes the OS might find it in cache.
389+
390+ // Attempt to load the library using dlopen
391+ #ifdef _WIN32
392+ void * handle = LoadLibrary (library_name );
393+ #else
394+ void * handle = dlopen (library_name , RTLD_LAZY );
395+ #endif
396+ if (!handle ) {
397+ return false;
398+ }
399+ // If we successfully loaded the library, we can use the library_name directly since it was in fact found that way.
400+ strncpy (resolved_path , library_name , MAX_PATH_LENGTH );
401+ resolved_path [MAX_PATH_LENGTH - 1 ] = '\0' ; // Ensure null-termination
402+ // Close the library handle. There's a reference counting mechanism in place, so this is safe.
403+ #ifdef _WIN32
404+ FreeLibrary (handle );
405+ #else
406+ dlclose (handle );
407+ #endif
408+ return true;
409+ }
410+
381411static bool FindSharedLibrary (const char * library_name , char * resolved_path ) {
382412
383413 if (!library_name || library_name [0 ] == '\0' ) {
@@ -419,19 +449,28 @@ static bool FindSharedLibrary(const char* library_name, char* resolved_path) {
419449 return false;
420450 }
421451#endif
452+ else {
453+
454+ #ifdef _WIN32
455+ bool found = FindInEnvVar ("PATH" , library_name , resolved_path );
456+ #else
457+ bool found = FindInEnvVar ("LD_LIBRARY_PATH" , library_name , resolved_path )
458+ #ifdef use_ld_so_conf
459+ || FindInLdSoConfFile ("/etc/ld.so.conf" , library_name , resolved_path , 0 )
460+ #endif
461+ || FindInStandardPaths (library_name , resolved_path );
462+ #endif
463+
464+ if (!found ) {
465+ found = JustTryDlOpenOnBasename (library_name , resolved_path );
466+ }
467+ return found ;
468+ }
469+ }
470+
471+
422472
423- #ifdef _WIN32
424- bool found = FindInEnvVar ("PATH" , library_name , resolved_path );
425- #else
426- bool found = FindInEnvVar ("LD_LIBRARY_PATH" , library_name , resolved_path )
427- #ifdef use_ld_so_conf
428- || FindInLdSoConfFile ("/etc/ld.so.conf" , library_name , resolved_path , 0 )
429- #endif
430- || FindInStandardPaths (library_name , resolved_path );
431- #endif
432473
433- return found ;
434- }
435474
436475// Function to attempt to resolve the library path
437476char * resolve_library_path (const char * library_name ) {
0 commit comments