title: MonoMac / Xamarin.Mac binding simple dylib not working
body: <p>I'm trying to bind some native code for use in MonoMac / Xamarin.Mac, but I'm not sure where I'm going wrong. I create a simple dylib to test with:</p>
  
  <p><em>nativelibrary.h</em>:</p>
  
  <pre><code>- (NSString *)echo:(NSString *)message;
  </code></pre>
  
  <p>I know that my library is fine, because I reference it and use it in an Objective-C / Cocoa application.</p>
  
  <p>Next, I try to generate the initial binding file using parse.exe:</p>
  
  <pre><code>mono parse.exe [path...]/nativelibrary.h 
  </code></pre>
  
  <p><strong>Problem #1</strong>
  No 'gen.cs' file is generated as per Miguel's <a href="https://github.com/mono/monomac/blob/master/BINDING-HOWTO" rel="nofollow noreferrer">guide</a></p>
  
  <p><strong>Problem #2</strong>
  Parse.exe does actually output something to the console, although it's missing my only method?</p>
  
  <pre><code>[BaseType (typeof (NSObject))]
  interface nativelibrary {
  }
  </code></pre>
  
  <p>Regardless, I go ahead and make my own gen.cs file, filling in the missing method manually:</p>
  
  <p><em>gen.cs</em>:</p>
  
  <pre><code>using MonoMac.Foundation;
  
  namespace ManagedConsumer
  {
      [BaseType (typeof (NSObject))]
      interface Binding 
      {
          [Export ("echo:")]
          string Echo(string message);
  
          // I also tried like this:
          // NSString Echo(NSString message);
      }
  }
  </code></pre>
  
  <p>Next, I try to create my binding DLL using bmac.exe:</p>
  
  <pre><code>mono bmac.exe -o="dynamiclibrary.dll" -d="MONOMAC" -r="System.Drawing" -v [path].../gen.cs 
  </code></pre>
  
  <p>This spits out a .dll which I reference in my MonoMac project.</p>
  
  <p>Finally, I add the .dylib itself to my MonoMac project, and specify the 'content' build action. I verify that the .dylib is copied to the 'Resources' directory of my bundle.</p>
  
  <p>I can instantiate an instance of my binding object no problem:</p>
  
  <pre><code>Binding b = new Binding();
  Console.WriteLine(b.ToString());
  </code></pre>
  
  <p><strong>Problem 3</strong> However, trying to call my method:</p>
  
  <pre><code>Binding b = new Binding();
  var result = b.Echo((NSString)"Hello, world");
  </code></pre>
  
  <p>results in an unmanaged crash:</p>
  
  <pre><code>Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
  Exception Codes: KERN_PROTECTION_FAILURE at 0x00000000bf74bffc
  </code></pre>
  
  <p>I have seen in another <a href="https://stackoverflow.com/questions/14230031/coreplot-monomac-bindings-crashing">question</a>, that we need to force the .dylib to load. So I try to insert this line into my <em>main.cs</em>, before <em>Application.Init()</em> is called:</p>
  
  <pre><code>Dlfcn.dlopen ("nativelibrary.dylib", 0);
  </code></pre>
  
  <p>But I get the same crash. Since the call to <em>dlopen</em> returns 0 rather than a valid pointer, I assume that the issue is in loading my dynamic library. I also tried to use the attribute:</p>
  
  <pre><code>[assembly:MonoMac.RequiredFramework("nativelibrary.dylib")]
  </code></pre>
  
  <p>But that only gets me:</p>
  
  <pre><code>System.Exception: Unable to load required framework: 'nativelibrary.dylib'
  </code></pre>
  
  <p>What am I doing wrong?</p>