Languages

Menu
Sites
Language
Xamarin : How to create static library of C/C++ ?

Hi !

 

We have our own c/c++ libraries for our application and want to port it on .net platform.

Our targets are tizen, android and iOS also.

Please guide us how to build the static library for c/c++.

 

Thanks.

Responses

8 Replies
Tizen .NET

Hi,
In Tizen Studio menu,  select File > New > Tizen Project > Template > Select Target device and Tizen version(e.g select Wearable 4.0 if you want to run your app on Galaxy Watch) > Native App > Shared Library.
To set the target architecture,
    in Project Explorer, right-click your project and select Properties.
    in Properties window, go to C/C++ Build > Tizen Settings and select arm in Platform > Architecture drop-down list, after then click Apply and Close button.
After building your codes and copying the output .so file to your C# project's lib directory, you can use it with P/Invoke(DllImport)

 

Ricky Kim

Do you mean the library shold be created in Tizen studio and moved to VS2019 ?

Sangwook Kim

Yes, the Tizen native SDK is not integrated with VS2017/VS2019. It is recommended to use the gcc compiler toolchain shipped with the Tizen SDK rather than building another cross-compilation setup.

Ricky Kim

Hi !

As your comment, I created SO file of my library and looks fine with objdump tools.

But in .net, I don't know how to integrate with shared project.

 

For android, AndroidNativeLibrary keyword in csproject file works for loading SO files perfectly and

I guess there is no problem except fot those issue.

 

I will summarize what I did.

 

1. In tizen studio, SO file is generated with platform Architecture ARM.

    the exported function is defined as 

              extern "C" void SomeFunction(int arg) { ... }

 

2. In VS, the library is copied to project workspace and included in project file list.

      in the property window,  the content was selected and always copy was chosen as seen in the .csproj file.

        <ItemGroup>
           <Content Include="lib\Debug\libMyLib.so">
               <CopyToOutputDirectory>Always</CopyToOutputDirectory>
           </Content>
       </ItemGroup>

 

3. In c# To load the function of SO file,

public static class MyCore

{

     [DllImport("libMyLib.so", EntryPoint = "SomeFunction")]
     public static extern IntPtr Info(int arg);
}
 
The result is that  MyCore is not loaded correctly.
 
Ricky Kim

In the above sentence, there is a just typing error.

 

public static extern IntPtr Info(int arg);  => public static extern void Info(int arg);

 

SO file can't be loaded anyway.

 

 

Tizen .NET

Could you try to just copy libMyLib.so file in lib directory of your Tizen project?

If you follow the below steps and still have problems, please share your simple sample app with us to reproduce the issue. (tizen.net@samsung.com)

 

1. create native library with Tizen Studio

inc/mytizensharedlibrary.h 
#ifndef _MYTIZENSHAREDLIBRARY_H_
#define _MYTIZENSHAREDLIBRARY_H_
 
/**
 * This header file is included to define _EXPORT_.
 */
#include <stdbool.h>
#include <tizen.h>
 
#ifdef __cplusplus
extern "C" {
#endif
 
// This method is exported from mytizensharedlibrary.so
EXPORT_API void tizenmytizensharedlibrary(int num);
 
#ifdef __cplusplus
}
#endif
#endif // _MYTIZENSHAREDLIBRARY_H_
 
src/mytizensharedlibrary.c 
/**
 * This file contains the exported symbol.
 */
#include "mytizensharedlibrary.h"
#include <stdio.h>
// This is an example of an exported method.
void
tizenmytizensharedlibrary(int num)
{
    printf("Hi Tizen! num(%d)\n", num);
}

 

 

2. Tizen csproj file

After you build native library in Tizen Studio, you need to copy .so file(libmytizensharedlibrary.so) in lib directory of your Xamarin.Tizen project(XXX/XXX.Tizen/lib)

<Project Sdk="Tizen.NET.Sdk/1.0.9">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>tizen40</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugType>portable</DebugType>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>None</DebugType>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="lib\" />
    <Folder Include="res\" />
  </ItemGroup>

  
  <ItemGroup>
    <ProjectReference Include="..\UseMyLibrary\UseMyLibrary.csproj" />
  </ItemGroup>

3. Call it by using DllImport

        protected override void OnCreate()
        {
            base.OnCreate();

            LoadApplication(new App());
            CallMyLibrary(2020);
        }

 

        [DllImport("libmytizensharedlibrary.so", EntryPoint = "tizenmytizensharedlibrary")]
        internal static extern void CallMyLibrary(int num);

 

Ricky Kim

Hi !

 

Thank you for your response.

 

One more question is related with csproj file.

In the second step, ProjectReference was defined with the csproj file again.

Does the csproj file mean recursive call the xamarin tizen project again ?

 

I guess the SO files needs to be defined in that sentence with proper field.

 

In android, "AndroidNativeLibrary", "abi" and "CopyToOutputDirectory" fields are used for SO file.

 

thanks.

 

 

 

Ricky Kim

Hi !

 

Thanks a lot.

 

Your comment was perfect and the library starts to work.

 

Thanks again !!!