• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by forestjay · Dec 18, 2015 at 09:31 PM · c#native pluginosxdllnotfoundexception

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?

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image caletbak2 · Mar 02, 2016 at 09:37 AM 0
Share

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!

avatar image caletbak2 caletbak2 · Mar 02, 2016 at 10:32 AM 0
Share

I have achieved to open the library by using the complete path to the MacOS folder inside the bundle contents.

My 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.

avatar image forestjay caletbak2 · May 06, 2016 at 08:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I include a framework in an OS X native bundle? 1 Answer

how to open Add To Contact with unity? 0 Answers

Audio Spatializer SDK. Source/Listener location matrix 0 Answers

How do I setup multi-display on 3 monitors in OSX? 0 Answers

Native DLL saying FileNotFoundException 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges