0

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)

  • 2
    You shouldn't reference this package explicitly, instead use [`Microsoft.Net.SDK.IL`](https://github.com/dotnet/runtime/issues/11411) – JL0PD Mar 11 '23 at 15:23

2 Answers2

2

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

JL0PD
  • 3,698
  • 2
  • 15
  • 23
1

If you are doing experiments, you can run .exe with Mono runtime, mono your.exe.

While .NET CLI should also accept it dotnet your.exe, you need to add supporting files, such as .dep.json, to specify information such as runtime.

BTW, like the comment indicated, use native IL support in .NET Core SDK is a better approach.

Lex Li
  • 60,503
  • 9
  • 116
  • 147