Xcode bundle compilation - failing to load in Unity

Hello

I’m having a weird problem compiling an osx bundle for unity: compiles just fine in xcode, no errors, but when I throw it at the Plugins folder, it looses the bundle icon (white lego brick) and becomes a folder, at which point I get the DLLNotFoundException error…anyone out there seen the same problem before?

Re-importing all sources does not help…

David

Ok this is now a DllNotFoundException error.

Having read this:

http://answers.unity3d.com/questions/2181/plugin-dependencies

I’m wondering: I’m trying to link libsndfile into a C/C++ bundle which is compiling just fine, linking to libsndfile_x86.a, and I’ve tried the macports libsndfile version (1.0.21_0) too…

But calling functions such as:

public class ZGUnity : MonoBehaviour
{
  // This tells unity to look up the function FooPluginFunction inside the plugin named "PluginName"
  [DllImport ("ZGUnity")]
     private static extern IntPtr zg_new_context(int numInputChannels, int numOutputChannels, int blockSize, float sampleRate,
          long function, long userData);

…gets me DLLNotFoundException

I can’t think of what’s going wrong, because the library should be statically linked…so I’ve tried using libsndfile.1.dylib…compiled, tried to load bundle, same DLL error.

Can anyone think of what might be going wrong?

Just to document my ‘progress’ (if you can call it that), and because I can’t find anything else on this in the forums…

the snippet of code above was born out of necessity, as I had to import functions from a bundle, and the function declarations had void pointers in them. Reading from this thread:

http://answers.unity3d.com/questions/2053/tell-me-how-to-use-unsafe-with-c

brought me (actually, Martin Roth working with me) to use IntPtr to try to wrap the dll function. We also read this thread from a little while back:

http://forum.unity3d.com/viewtopic.php?t=6153

But, our implementation so far seems to fail with dll not found again. Here’s the complete c# script calling the bundle (which we’ve checked contains all the symbols it should):

using UnityEngine;
using System;
using System.Runtime.InteropServices;

public class ZGUnity : MonoBehaviour
{
  // This tells unity to look up the function FooPluginFunction inside the plugin named "PluginName"
  [DllImport ("ZGUnity")]
     private static extern IntPtr zg_new_context(int numInputChannels, int numOutputChannels, int blockSize, float sampleRate,
          long function, long userData); // void (*function)(int function, void *userData, void *ptr)

  [DllImport ("ZGUnity")]
       private static extern IntPtr zg_new_graph(IntPtr context, string directory, string patch);

  [DllImport ("ZGUnity")]
       private static extern void zg_attach_graph(IntPtr context, IntPtr graph);

  [DllImport ("ZGUnity")]
       private static extern void zg_process(IntPtr context, float[] inputBuffer, float[] outputBuffer);

  [DllImport ("ZGUnity")]
       private static extern void zg_delete_context(IntPtr context);

  void Awake ()
  {
    int numInputChannels = 2;
    int numOutputChannels = 2;
    int blockSize = 256;
    float sampleRate = 44100.0f;
    float[] inputBuffer = new float[blockSize * numInputChannels];
    float[] outputBuffer = new float[blockSize * numOutputChannels];

    IntPtr context = zg_new_context(numInputChannels, numOutputChannels, blockSize, sampleRate, 0, 0);

    IntPtr graph = zg_new_graph(context, "/Users/dpc/GamesDevelopment/zengardengit/zengardenunityNormal/Assets/Plugins", "test.pd");

Debug.Log("Patch:" + graph);

    zg_attach_graph(context, graph);
	
    while (true) {
      zg_process(context, inputBuffer, outputBuffer);
    }
	
    zg_delete_context(context);
  }
}

What are we doing wrong? Am I not correct in thinking that we can’t use ‘unsafe’ as there’s no way to invoke the unsafe compiler flag in Unity, and in any case it would be best to marshal?

There are two things I can think off:

  1. the function you try to hook in are all C, right, not C++?

  2. The library needs to be added in xcode post export, not to unity. You can not use unmanaged code within your project with iphone, as it hooks it up upon the compilation in xcode only

yup all c

I didn’t know this, but I’ve tried with 3.0 beta, which has both iphone and normal desktop build targets, and the desktop version had the same dll not found error…

Just wondering: people must do this all the time, right? What’s best practice for importing functions from c and getting around the unsafe option problem?

on the desktop it must be a dll (unmanaged) on windows, bundle on osx actually, static libraries are not supported at all.

for the iphone it has to be added on the xcode side, normally as extern “C” functions in an .mm file in the xcode project (see manual - advanced iphone topics - plugins). in this case you can just use DLL import and marshaling, but the DLLImport will look slightly different (on unity iphone you use __Internal)

crossplatform plugins don’t exist as the requirements and functionalities available differ from platform to platform

Thanks that helps. Where did you find out that on iphone you’d use __Internal? I’m not sure about usage, and Unity 3.0 beta has all targets, so since it’s not unity pro versus unity iphone anymore, I’m just wondering where the differences in things like DLLImport functions are spelled out…can’t find it in the scripting reference…