How to use dlopen on OS X?
I'm working on making a set of native libraries for various platforms (OS X, Windows, iOS, and Android). On OS X, I have a bundle. For Windows, I opened the native dll with the following code:
internal static void LoadNativeDll(string FileName)
{
if (lib != IntPtr.Zero)
{
return;
}
lib = LoadLibrary(FileName);
if (lib == IntPtr.Zero)
{
Debug.LogError("Failed to load native library! ");
}
}
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr LoadLibrary(
string lpFileName
);
I am trying the following for OS X:
public static IntPtr LoadLibrary(string fileName) {
return dlopen(fileName, RTLD_NOW);
}
public static void FreeLibrary(IntPtr handle) {
dlclose(handle);
}
public IntPtr GetProcAddress(IntPtr dllHandle, string name) {
// clear previous errors if any
dlerror();
var res = dlsym(dllHandle, name);
var errPtr = dlerror();
if (errPtr != IntPtr.Zero) {
throw new Exception("dlsym: " + Marshal.PtrToStringAnsi(errPtr));
}
return res;
}
const int RTLD_NOW = 2;
[DllImport("libdl.so")]
private static extern IntPtr dlopen(String fileName, int flags);
[DllImport("libdl.so")]
private static extern IntPtr dlsym(IntPtr handle, String symbol);
[DllImport("libdl.so")]
private static extern int dlclose(IntPtr handle);
[DllImport("libdl.so")]
private static extern IntPtr dlerror();
This is based on a blog I found. OS X is UNIX based, but there does not appear to be a version of libdl.so compiled for OS X and thus get the error:
DllNotFoundException: libdl.so
Even if one did exist, would it need to be packaged within the asset folder? MacMono seems to be an option that used to exist. Help, how do people load libraries in OS X?
Answer by forestjay · Dec 18, 2015 at 10:36 PM
I fixed this by using libdl.dylib instead of libdl.so . I got this idea from a Wikipedia article. I also wrote a blog with more details.
Hi,
I am trying to do the same as you with a native OSX plugin. The point is that I am not able to load the bundle, so the LoadLibrary is always returning a 0 Intptr value. Is it needed to have the bundle in the plugins folder of Unity and with OSX library option checked in order to find the library, or just using the complete path to the bundle would be enough?
Thanks!
I have achieved to open the library by using the complete path to the $$anonymous$$acOS folder inside the bundle contents.
$$anonymous$$y problem now is that after calling dlclose in the OnApplicationQuit method, Unity IDE freezes when I try to play the scene again.
Is this also happening with your code?
Cheers.
Hi, sorry I only just saw this. I didn't have notifications enabled. Did this get solved? The IDE does not freeze for me. I suspect an exception is being thrown which could probably be caught by a debugger.
Your answer
Follow this Question
Related Questions
My unity project only works on certain computers 0 Answers
Changing Impulse Response AudioClip in Convolution Reverb Demo plugin of the Native Audio Plugin SDK 2 Answers
Are you missing an assembly reference? Android build 0 Answers
How do I include a framework in an OS X native bundle? 1 Answer
how to open Add To Contact with unity? 0 Answers