DllNotFoundException first and then EntryPointNotFoundException when calling pure C function from C #

I am trying to use some C code in Unity3D pro.

My basic approach is to (1) build the C source code into a static library (2) create an empty bundle and link to the library (3) import the package into Unity 3D pro (as a plugin).

I have mostly followed this tutorial .

It worked for me for a while until recently, when I was updating my C code and recompiling and rebuilding the library and bundle everything goes wrong ...

When I first imported the package, I got a DllNotFoundException .

At this point, my package project was just a bunch of libraries - a library built from my source code (which is an .a file) and a few other libraries that I believe depends on the .a library (other .dylib libraries).

I added one test.c file to the package. The test .c file contains only two lines:

#include <stdio.h>
#include "ccn.h"

char* say()
{
    return "hi";
}

      

in which ccn.h is a header file from my C source code.

After I rebuilt the package the DllNotFoundExeption disappeared, but I got an EntryPointNotFoundException .

That's what I think:

  • Since Xcode is so smart, I guess by adding a simple test.c file, Xcode modified some of my package settings for me. And I believe that is the reason the DllNotFoundException went away in the first place. Am I thinking right?
  • I think in order to solve the EntryPointNotFound problem I will also have to change some of the package project parameters, but I don't know how ... Please help me ...

Many, many thanks!

Edit

  • I saw this thread suggesting that since Xcode uses g ++ to compile I would need extern "C" even if my unmanaged code is in pure C. I changed my target setting in Xcode4 to " Compiler for C / C ++ / Objective -C "from Apple LLVM 3.0 to LLVM GCC 4.2 , but I still get the same exception.
  • I've also seen this monograph documentation , which says mono uses GLib to load libraries, and there is a bug in Glib on Mac OS X that doesn't recognize .dylib libraries. I removed the .dylib libraries and tried again, but before getting the same exception.
  • Also Apple Documentation says that every downloadable bundle should have a main class, and if the user doesn't specify which class is the main class, NSBundle will use the first class shown in the Xcode project as the main class. Well, I think the set I created is a downloadable package, but since it is just a static library built in C, it literally doesn't have any class. When I looked at my info.plist project , the Principal Class entry is just empty. Could this be the problem? If so, how to solve it?
  • Also I saw something in Apple documentation that I don't quite understand:

    If your download package is a plug-in, the main application developer usually provides an interface for the plug-in architecture to the framework. This structure usually contains a class that the entire plugin inherits from the main classes or protocol (formal or informal) for the main plugin class to adopt.

    I'm pretty sure the downloadable package I'm building is a plugin. I am importing a package into Unity3D. Does this mean that I have to define the main class from Unity? How?

+3


source to share


1 answer


The problem has a simple solution:

try any function that gives you the EntryPointNotFoundException property in the test.c. file in the first place.

By "try out" I mean that the function is called in your main () and tests it first in C.

In my case, the function that throws the EntryPointNotFoundException is called ccn_run (). So I did the following things:

  • add main () function to test.c
  • enter ccn_run () in main () function
  • add command line target to Xcode project and test it

I tried this and the function works fine in C, now the magic is that when I rebuild the package (without removing the main () function and calling ccn_run () inside it), Unity can call ccn_run () without issue!



I couldn't explain why this solves my problem, but this has been working for almost a month now. More recently, Xcode updated to 4.3.2 and I tried the same approach again, but it still works. Perhaps adding a main () function helps define the entry point?

Finally, I've documented the whole process of importing C code into Unity and I'm happy to share here: https://docs.google.com/document/d/1vAeupNQlBTY3y3Bma5Jo_Hs4JRYm6FoEc4vQN0WBGNg/edit

My example code:

Hope this answer, especially my tutorial, helps!

+2


source







All Articles