Maker of open-source software and hardware.

Transpiling C# to a cross-platform C static library

Feature image

After my post from yesterday, I discovered that the CoreRT docs have some tutorials that work and some that don't, at least on Ubuntu 18.04. The difference between those are that the second one requires you to build the compiler yourself, whereas the first one adds the compiler as a package.

I've attempted to take this MIT-licensed LZO implementation and compile it as a shared library. At the time of writing, the documentation for building a shared library using CoreRT is still an open PR, so I used this commit as reference.

First I made sure I had all the prerequisites in place. This includes installing the .NET SDK and installing the following dependencies:

sudo apt-get install cmake clang-3.9 libicu55 uuid-dev libcurl4-openssl-dev zlib1g-dev libkrb5-dev

Then I ran dotnet new nuget to add a nuget config file and edited it to look like this (adding the dotnet-core and nuget.org package sources):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
 </packageSources>
</configuration>

I then edited the .csproj file to look like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="1.0.0-alpha-*" />
  </ItemGroup>

</Project>

I then ran dotnet publish /p:NativeLib=Static -r linux-x64 to generate the static library. It's still 15MB in size, so I need to figure out how to strip out the unnecessary stuff. But first I need to actually export methods from the library to be able to use them and see if it actually works.