-->

How to use native C++ libraries in Mono for Androi

2020-08-22 07:31发布

问题:

I am interested in using Mono for Android and MonoTouch to build mobile applications that can make use of a large number of C++ native libraries that I have. Can I get some specific instructions on how to call methods or incorporate the code into my final mobile app on iOS or Android?

I have looked at the Xamarin documentation online it is pretty cryptic to me as someone who is new to programming mobile apps (Mono for Android and MonoTouch). In the case of building a monodroid app in Visual Studio (which is what I am most familiar with), what are specific steps I need to call a native function from my MonoDroid program. For the C libraries, I can compile in either .DLL (dynamic) or .LIB (static) form. I am also under the impression that dynamic libraries cannot be used for mobile apps.

回答1:

It is theoretically possible to bind C++ libraries using P/Invokes, but this is not a particularly nice way (since you will be binding to the mangled function names, which are quite unintelligible and will likely change between compilers/OSes).

The recommended way of accessing C++ libraries is to create a C API and use P/Invoke (you can find a lot of information about how to do P/Invoking on the web, here is one place to start: http://mono-project.com/Dllimport).

Also, for MonoTouch it is not possible to use dynamic libraries (this is an Apple restriction), you have to use static libraries. I do not know if there are any restrictions like this for Mono for Android.



回答2:

The best choice is to use Swig to create a binding for your C++ library that you can access from C#.

Here is a description:

http://www.swig.org/Doc1.3/CSharp.html



回答3:

Cxxi doesn't work with monotouch. As I ran to this same issue last week, I blogged about how to get SWIG works for monotouch:

http://blog.reblochon.org/2013/01/c-bindings-for-monotouch-using-swig.html



回答4:

This blog post from Miguel is an interesting read on the topic.

CXXI: Bridging the C++ and C# worlds.

I do not know if CXXI is available a this time, but it is a project to keep an eye on and support.