4

I am new to using .NET, but I am interested in using MonoTouch/Droid to write mobile applications that could share some core code.

I have many C style API libraries I wish to use, for example libxml2. How do I call these native library methods in Mono? Can I use the same binaries compiled from Windows if I am developing in Windows? Can I use the Windows binaries if I am developing in OS X?

user1027169
  • 2,627
  • 3
  • 27
  • 50
  • 2
    Mono comes out of the box with an XML library. And no, you can't use native Windows binaries in OS X (or iOS or Android, for that matter). – Marcelo Cantos Apr 11 '12 at 00:26

1 Answers1

7

How do I call these native library methods in Mono?

You use p/invoke (platform invoke) to call native C code from any .NET language. You need to write those declarations (of find someone who did it before you) to use the native libraries. Like @Marcelo commented there are often very good, much easier to use .NET alternatives to most C libraries.

This will work on MonoTouch too. However you'll need provide a static library (.a on OSX, like a .lib on Windows) since Apple iOS does not allow linking with user-provided dynamic libraries (.dylib on OSX/iOS and .dll on Windows).

Can I use the same binaries compiled from Windows if I am developing in Windows?

Windows produced binaries should run fine with Mono on Windows. You can use Microsoft .NET on Windows too.

Can I use the Windows binaries if I am developing in OS X?

If the binaries are .NET compiled code then yes - as long as your p/invoke declrations are portable (e.g. 32 vs 64 bits types).

If the binaries are native code then no. Remember that Mono is not a Windows emulator. It runs CIL code, inside .EXE or .DLL, but it won't run native Windows code or provide access to Windows native API (outside Windows).

poupou
  • 43,413
  • 6
  • 77
  • 174
  • Could I possible get some further direction on how referencing a static native library works? Resources I have [found](http://msdn.microsoft.com/en-us/library/aa446536.aspx) seem to only specify for linking dynamic libraries. – user1027169 Apr 13 '12 at 19:17
  • I just found [this post](http://stackoverflow.com/questions/729562/how-to-compile-c-sharp-application-with-c-static-library) that says that linking static libraries is not possible in .NET. How then can I use my C++ libraries in MonoTouch? – user1027169 Apr 13 '12 at 19:19
  • The post is right: it's **not** possible with **Microsoft** .NET. However it **is** possible with Mono (when you embed the runtime) and with MonoTouch (since it's a requirement for iOS). – poupou Apr 13 '12 at 19:28