.netlinuxmacoscililasm

How to compile CIL code (intermediate code used on .NET) using ilasm for Linux and OSX?


I used the runtime.win-x64.Microsoft.NETCore.ILAsm nuget to get ilasm.exe to compile my CIL codes. It compiles everything correctly for the windows .exe PE, but apparently it doesn't compile for Linux and MacOS...

I know that using .NET 5.0 or newer with the dotnet --runtime command is able to compile for any supported OS. How to do this using CIL instead of C#? I thought it was using ilasm.exe, but apparently it's not possible, so what would be the solution for that?

Note: I already used the linux ilasm and the code compiled on linux was a .exe PE, an extension and format that doesn't work on linux (linux uses ELF format)


Solution

  • In order to compile MSIL I use following template:

    ProjectName
    |- ProjectName.ilproj
    |- Program.il
    

    ProjectName.ilproj content:

    <Project Sdk="Microsoft.Net.Sdk.il/7.0.0">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net7.0</TargetFramework>
      </PropertyGroup>
    </Project>
    

    Program.il content:

    .assembly extern mscorlib {}
    .assembly 'App' {}
    
    .class private auto ansi beforefieldinit abstract sealed Program extends [mscorlib]System.Object {
        .method private hidebysig static void Main ( string[] args) cil managed {
            .entrypoint
    
            ldstr "hw"
            call void [mscorlib]System.Console::WriteLine(string)
    
            ret
        }
    }
    

    This setup allows me to compile programs without any problems. Commands dotnet build -r linux-x64 --self-contained -c release and dotnet build -r win-x64 --self-contained -c release seems to work and produce some platform-specific output