Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use native C++ libraries in Mono for Android (monodroid) and MonoTouch

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.

like image 243
user1027169 Avatar asked Apr 13 '12 19:04

user1027169


3 Answers

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.

like image 156
Rolf Bjarne Kvinge Avatar answered Nov 10 '22 00:11

Rolf Bjarne Kvinge


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

like image 23
miguel.de.icaza Avatar answered Nov 09 '22 22:11

miguel.de.icaza


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

like image 3
Stephane Delcroix Avatar answered Nov 09 '22 22:11

Stephane Delcroix