There are many libraries developed using C or C++ which can be brought to bear when creating Xamarin-based mobile apps such as those leveraged by Microsoft’s Hyperlapse Mobile and Pix Camera in order to achieve high performance image processing. This article, “Use C/C++ Libraries With Xamarin“, walks through developing a C# wrapper for a given native library and an approach for keeping it decoupled from the mobile application(s) enabling greater re-use and segregation of development responsibilities.

The Approach

The approach has four stages:

  1. Build the native C/C++ libraries for each platform using the tools and processes already in place (if required).
  2. Wrap the native libraries within platform-specific Xamarin library targets (alongside a portable target).
  3. Package the wrapper libraries with NuGet, exploiting a technique called ‘bait-and-switch’.
  4. Use the NuGet in your Xamarin application.

Although straightforward, implementing the approach requires familiarity with several different concepts: C/C++ and P/Invoke, NuGet Dynamics, and Xamarin. The article goes through each stage in detail, ensuring that even if you’re not familiar with NuGet internals, you will be able to configure it for the  ‘bait-and-switch’ technique.

High-level overview of technique

The Four Stages

  1. The first stage is the native C/C++ build stage and is likely already defined and managed by the developers of the native library. In the case of third-party libraries, it is often provided as a set of pre-built binaries and header files.
  2. The second stage is wrapping the native libraries in a C# project in the appropriate “bait-and-switch” form. The output is a set of managed libraries, one for each of the supported native platforms alongside a platform-agnostic library such as one that targets .NET Standard. All platform-specific code that is needed to access the native libraries is dealt with in the wrapper like pre-processor defines, library naming conventions, etc. There is no burden to the developers of the mobile application(s) consuming the NuGet package since knowledge of these inner workings are not required.
  3. The third stage takes these managed libraries, packages them with NuGet, and makes the NuGet package available to the application developer. The walkthrough does this by simply publishing to a local directory. Publishing to a private feed is another option that is especially appealing to distributed teams.
  4. The fourth stage is consuming the NuGet from the Xamarin application. This is simply a matter of referencing the NuGet and calling the library functionality. It is as easy as accessing a C# library since the 2nd stage exposes the native C/C++ functionality indirectly via standard managed objects and since the 3rd stage makes providing the appropriate native library transparent. The native library’s functionality is called from shared code, not from platform-specific code.

The Walkthrough

The walkthrough comes with a complete, albeit simple, sample including the basic throw-away script used to build the native libraries. The unmanaged world of C/C++ in addition to the managed world of C# can initially seem difficult to bridge. A real-world example will likely be more complex with challenges such as exception handling, the marshalling of more complex types, and linking with other dependency libraries that are not covered in the sample. However, the approach itself can enable existing C/C++ libraries to be independently wrapped and subsequently incorporated into Xamarin-based solutions via NuGet in a cross-platform manner in the same way as other managed C# libraries.

See It In Action

Watch James Montemagno and Mike Parker from the Microsoft Mobile Customer Advisory Team on The Xamarin Show. Learn how to get started using C++ libraries in iOS and Android applications: