How to use a Xamarin Code to Call pre-defined Libraries of C/C++

Xamarin a well-known cross-platform app development environment which permits app developers to build it in a way to run the same mobile application on the multiple mobile OS Android and iOS with the same grace. For the buildup of an application Xamarin technology uses C# coding environment, however, it may be noticed that a few codes are very complex and large in size, which creates an irritation while porting it into the C# coding environment. Thus, here we are going to showcase you how to use an existing code component library of objective C for the creation of a Xamarin.iOS app and the use of the existing Java component libraries in the creation of an app in Xamarin.Android.

To compile your developed code for the Android and iOS, you need source code. To go ahead with this, just create a new project, as shown in the image.


Alongside this, also install a visual studio package (latest version) through which you can create, compile and debug the project with ease. Here, I am using the visual studio professional of version 2015. See the picture below:


Now you will get three project options in which are Android, iOS, and multi-platform respectively. A multi-platform project is also known as a shared project which can be accessed on each mobile OS.

Here, it is must understand the shared project concept as the code that will be generated for a shared project did not give the result in DLL, while it can easily adopt any type and format of a file to access the particular code. The code is compiled in the context of the referencing project and if the code is not get referenced, it will not be compiled. For this, it is required to have call in each code of the platform specific project.

To better understand the concept create a simple function clib_add_internal() in your shared project as:

#pragma once

#include <stdint.h>

Int32_t clib_add_internal(int32_t left, int32_t right):

#include “clib.h”

Int32_t clib_add_internal(int32_t left, int32_t right)


Return left + right;


This is a platform-specific code which is called from a function.

Well, the code to calls from .Net is using P/Invoke while C++/CLI is unavailable for Android and iOS. Hence the basic rules apply for the interface code like the code will remain in C, or if you are using C++ coding environment you can use a plain function or a static method which contain only POC parameters and return values.

In a Xamarin.iOS project the result needs a static library (.a file) because iOS did not grant a facility to dinamically load the particular code.


While on the other side Android uses a dynamic library(.so).


by admin

Leave a Reply

Your email address will not be published. Required fields are marked *