[英]First DllNotFoundException then EntryPointNotFoundException while calling pure C function from C#
I am trying to use some C code in Unity3D pro. 我想在Unity3D pro中使用一些C代码。
My basic approach is to (1) build C source code into a static libary (2) create an empty bundle and link against the library (3) import the bundle into Unity 3D pro (as a plugin).
我的基本方法是(1)将C源代码构建到静态库中(2)创建一个空包并链接到库(3)将包导入Unity 3D pro(作为插件)。
I was basically following this tutorial . 我基本上是按照本教程 。
It has been working for me for a while until recently when I was updating my C code and recompiling and rebuilding the library and the bundle, everything goes wrong... 它一直在为我工作一段时间,直到最近我更新我的C代码并重新编译和重建库和捆绑包,一切都出错了......
When I first imported the bundle, I got a DllNotFoundException . 当我第一次导入捆绑包时,我得到了一个DllNotFoundException 。
At that stage my bundle project was merely a pile of libraries - the library built from my source code (which is a .a file) and a few other libraries that I think the .a library depends on (the other libraries have .dylib extension). 在那个阶段,我的bundle项目只是一堆库 - 从我的源代码(一个.a文件)构建的库和我认为.a库所依赖的一些其他库(其他库具有.dylib扩展名) )。
I added one test.c file to the bundle. 我在bundle中添加了一个test.c文件。 The test .c file contains only two lines:
测试.c文件只包含两行:
#include <stdio.h>
#include "ccn.h"
char* say()
{
return "hi";
}
in which ccn.h is a header file from my C source code. 其中ccn.h是我的C源代码中的头文件。
After I re-built the bundle, the DllNotFoundExeption was gone, but I got an EntryPointNotFoundException . 在我重新构建捆绑包之后, DllNotFoundExeption消失了,但我得到了一个EntryPointNotFoundException 。
Lots and Lots of thanks! 很多和很多的感谢!
Also I saw something on Apple's documentation that I don't quite understand: 另外我在Apple的文档中看到了一些我不太明白的东西:
If your loadable bundle is a plug-in, the host application developer usually provides an interface for the plug-in architecture in a framework.
如果可加载包是一个插件,则主机应用程序开发人员通常在框架中为插件体系结构提供接口。 This framework typically contains a class that all plug-in principal classes inherit from, or a protocol (formal or informal) for the plug-in's principal class to adopt.
此框架通常包含所有插件主体类继承的类,或者插件的主体类要采用的协议(正式或非正式)。
I am pretty sure the loadable bundle I create is a plugin. 我很确定我创建的可加载包是一个插件。 I am importing the bundle into Unity3D.
我正在将捆绑包导入Unity3D。 Does that mean I should define a principal class from Unity?
这是否意味着我应该从Unity定义一个主要类? How?
怎么样?
The problem has a simple solution: 问题有一个简单的解决方案:
try out whatever function that is giving you the EntryPointNotFoundException in the test.c file first. 尝试先在test.c文件中为您提供EntryPointNotFoundException的任何函数。
By "try out" I mean, have the function called in your main() and test it out in C first. 通过“试用”我的意思是,在main()中调用函数并首先在C中测试它。
In my case, the function that throws out the EntryPointNotFoundException is called ccn_run(). 在我的例子中,抛出EntryPointNotFoundException的函数称为ccn_run()。 So I did the following things:
所以我做了以下事情:
I tried it out and the function is working alright in C, now the magical thing is that when I rebuild the bundle (without deleting the main() function and the ccn_run() call inside it), Unity can call the ccn_run() function without problem! 我试了一下,函数在C中工作正常,现在神奇的是当我重建bundle时(不删除main()函数和ccn_run()调用),Unity可以调用ccn_run()函数没有问题!
I couldn't explain why this solves my problem, but this has been working for me for almost a month now. 我无法解释为什么这可以解决我的问题,但这已经为我工作了近一个月了。 Not long ago Xcode upgraded to 4.3.2 and I tried the same approach again, still works.
不久前Xcode升级到4.3.2并且我再次尝试了相同的方法,仍然有效。 I guess perhaps adding a main() function helps defining the entry point?
我想也许添加一个main()函数有助于定义入口点?
Finally, I documented the whole process of importing C code to Unity and I am glad to share here: https://docs.google.com/document/d/1vAeupNQlBTY3y3Bma5Jo_Hs4JRYm6FoEc4vQN0WBGNg/edit 最后,我记录了将C代码导入Unity的整个过程,我很高兴在此分享: https : //docs.google.com/document/d/1vAeupNQlBTY3y3Bma5Jo_Hs4JRYm6FoEc4vQN0WBGNg/edit
My code sample: 我的代码示例:
Hope this answer especially my tutorial helps! 希望这个答案特别是我的教程帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.