bflattened / bflat

C# as you know it but with Go-inspired tooling (small, selfcontained, and native executables)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`bflat build-il` can't target .NET 7 and .NET 8

opened this issue · comments

Doesn't work:

bflat build-il --stdlib=none -r:"C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.15\mscorlib.dll" Program.cs -o Test.exe

bflat build-il --stdlib=none -r:"C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.1\mscorlib.dll" Program.cs -o Test.exe

Worked:

bflat build-il --stdlib=none -r:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll Program.cs -o Test.exe

Or do I need .NET SDK?

Update: I tested with all of the mscorlib.dll of .NET SDK, same result.

Duplicate of #170.

Duplicate of #170.

This is not a duplicate of #170. #170 is about running compiled program with bflat build-il. This is about compiling with bflat build-il --stdlib=zero and target .NET 7 or .NET 8 runtime by referencing mscorlib.dll directly.

Duplicate of #170.

This is not a duplicate of #170. #170 is about running compiled program with bflat build-il. This is about compiling with bflat build-il --stdlib=zero and target .NET 7 or .NET 8 runtime by referencing mscorlib.dll directly.

Pretty sure that's the same problem. The generated executable needs a runtimeconfig.json and needs to be executed using dotnet exec.

The only reason why this works for .NET framework is because the operating system knows how to load the correct .NET runtime into the process. The OS has no special handling for .NET 5+.

Duplicate of #170.

This is not a duplicate of #170. #170 is about running compiled program with bflat build-il. This is about compiling with bflat build-il --stdlib=zero and target .NET 7 or .NET 8 runtime by referencing mscorlib.dll directly.

Pretty sure that's the same problem. The generated executable needs a runtimeconfig.json and needs to be executed using dotnet exec.

The only reason why this works for .NET framework is because the operating system knows how to load the correct .NET runtime into the process. The OS has no special handling for .NET 5+.

No. It's not the same problem. Because with this issue, the compilation by bflat build-il failed, not the compiled program failed to run. Error message (copied from Command Prompt, so difficult to read, this is the reason why I didn't post it from the beginning):

C:\test\fibonacci\src>bflat build-il --stdlib=none -r:
"C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.1\mscorlib.dll" Progra
m.cs -o Test.exe
C:\test\fibonacci\src\Program.cs(7,11): error CS0518:
Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(8,33): error CS0518:
Predefined type 'System.String' is not defined or imported
C:\test\fibonacci\src\Program.cs(8,23): error CS0518:
Predefined type 'System.Void' is not defined or imported
C:\test\fibonacci\src\Program.cs(24,39): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(24,24): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(36,59): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(36,24): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(36,24): error CS1069:
 The type name 'IEnumerable<>' could not be found in the namespace 'System.Colle
ctions.Generic'. This type has been forwarded to assembly 'System.Private.CoreLi
b, Version=0.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' Consider a
dding a reference to that assembly.
C:\test\fibonacci\src\Program.cs(36,36): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(9,13): error CS0518:
Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(9,13): error CS0103:
The name 'Console' does not exist in the current context
C:\test\fibonacci\src\Program.cs(9,31): error CS0518:
Predefined type 'System.String' is not defined or imported
C:\test\fibonacci\src\Program.cs(10,13): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(10,13): error CS0103:
 The name 'Console' does not exist in the current context
C:\test\fibonacci\src\Program.cs(10,31): error CS0518:
 Predefined type 'System.String' is not defined or imported
C:\test\fibonacci\src\Program.cs(11,13): error CS0518:
 Predefined type 'System.String' is not defined or imported
C:\test\fibonacci\src\Program.cs(11,26): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(11,26): error CS0103:
 The name 'Console' does not exist in the current context
C:\test\fibonacci\src\Program.cs(12,13): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(13,17): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(13,16): error CS0518:
 Predefined type 'System.Boolean' is not defined or imported
C:\test\fibonacci\src\Program.cs(14,27): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(14,27): error CS1069:
 The type name 'ArgumentException' could not be found in the namespace 'System'.
 This type has been forwarded to assembly 'System.Private.CoreLib, Version=0.0.0
.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' Consider adding a referenc
e to that assembly.
C:\test\fibonacci\src\Program.cs(14,51): error CS0518:
 Predefined type 'System.String' is not defined or imported
C:\test\fibonacci\src\Program.cs(14,23): error CS0518:
 Predefined type 'System.Exception' is not defined or imported
C:\test\fibonacci\src\Program.cs(16,28): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(17,21): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(18,17): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(18,17): error CS0103:
 The name 'Console' does not exist in the current context
C:\test\fibonacci\src\Program.cs(19,17): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(19,17): error CS0103:
 The name 'Console' does not exist in the current context
C:\test\fibonacci\src\Program.cs(19,31): error CS0518:
 Predefined type 'System.Char' is not defined or imported
C:\test\fibonacci\src\Program.cs(21,6): error CS0518:
Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(21,6): error CS0103:
The name 'Console' does not exist in the current context
C:\test\fibonacci\src\Program.cs(21,24): error CS0518:
 Predefined type 'System.String' is not defined or imported
C:\test\fibonacci\src\Program.cs(25,21): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(25,16): error CS0518:
 Predefined type 'System.Boolean' is not defined or imported
C:\test\fibonacci\src\Program.cs(25,16): error CS0518:
 Predefined type 'System.Boolean' is not defined or imported
C:\test\fibonacci\src\Program.cs(26,24): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(28,26): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(28,21): error CS0518:
 Predefined type 'System.Boolean' is not defined or imported
C:\test\fibonacci\src\Program.cs(28,21): error CS0518:
 Predefined type 'System.Boolean' is not defined or imported
C:\test\fibonacci\src\Program.cs(29,24): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(32,24): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(32,39): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(32,44): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(32,59): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(37,17): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(37,25): error CS0518:
 Predefined type 'System.Int32' is not defined or imported
C:\test\fibonacci\src\Program.cs(37,28): error CS0518:
 Predefined type 'System.Boolean' is not defined or imported
C:\test\fibonacci\src\Program.cs(37,28): error CS0518:
 Predefined type 'System.Boolean' is not defined or imported
C:\test\fibonacci\src\Program.cs(38,30): error CS0518:
 Predefined type 'System.Object' is not defined or imported
C:\test\fibonacci\src\Program.cs(7,11): error CS1729:
'object' does not contain a constructor that takes 0 arguments
error CS0518: Predefined type 'System.Attribute' is not defined or imported
error CS0518: Predefined type 'System.Attribute' is not defined or imported
error CS0518: Predefined type 'System.Int32' is not defined or imported
error CS0656: Missing compiler required member 'System.AttributeUsageAttribute..
ctor'
error CS0656: Missing compiler required member 'System.AttributeUsageAttribute.A
llowMultiple'
error CS0656: Missing compiler required member 'System.AttributeUsageAttribute.I
nherited'

The error message for .NET 7 is similar.

Program.cs, extracted from CSharp.lua test:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;

namespace Test {
    class Program {
        public static void Main(string[] args) {
            Console.WriteLine("Print Fibonacci sequence");
            Console.WriteLine("Input n:");
            string num = Console.ReadLine();
            int count;
            if(!int.TryParse(num, out count)) {
                throw new ArgumentException(num + " is unlawful");
            }
            var sequence = FibonacciSequence(count);
            foreach(int v in sequence) {
                Console.Write(v);
                Console.Write(',');
            }
	    Console.WriteLine("");
        }

        private static int FibonacciN(int n) {
            if(n == 0) {
                return 0;
            }
            else if(n == 1) {
                return 1;
            }
            else {
                return FibonacciN(n - 1) + FibonacciN(n - 2);
            }
        }

        private static IEnumerable<int> FibonacciSequence(int n) {
            for(int i = 0; i < n; ++i) {
                yield return FibonacciN(i);
            }
        }
    }
}

Ah, if you're only passing -r:mscorlib.dll then the problem is that. mscorlib.dll in .NET 5+ only has type forwarders but no type definitions. The compiler is complaining that it cannot find System.Object because there's no definition for it.

Try passing -r:C:\Program Files\dotnet\sdk\8.0.101\ref\mscorlib.dll -r:C:\Program Files\dotnet\sdk\8.0.101\ref\netstandard.dll

You can also just drop mscorlib. It's just a backwards compatibility wart.

bflat build-il --stdlib=zero -r:"C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.1\mscorlib.dll" -r:"C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.1\netstandard.dll" Program.cs -o Test.exe

Output:

C:\test\fibonacci\src\Program.cs(36,24): error CS1069:
 The type name 'IEnumerable<>' could not be found in the namespace 'System.Colle
ctions.Generic'. This type has been forwarded to assembly 'System.Private.CoreLi
b, Version=0.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' Consider a
dding a reference to that assembly.
C:\test\fibonacci\src\Program.cs(11,34): error CS0117:
 'Console' does not contain a definition for 'ReadLine'
C:\test\fibonacci\src\Program.cs(13,21): error CS0117:
 'int' does not contain a definition for 'TryParse'
C:\test\fibonacci\src\Program.cs(14,27): error CS1069:
 The type name 'ArgumentException' could not be found in the namespace 'System'.
 This type has been forwarded to assembly 'System.Private.CoreLib, Version=0.0.0
.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' Consider adding a referenc
e to that assembly.
C:\test\fibonacci\src\Program.cs(14,23): error CS0518:
 Predefined type 'System.Exception' is not defined or imported
C:\test\fibonacci\src\Program.cs(18,31): error CS1503:
 Argument 1: cannot convert from 'int' to 'char'

Use --stdlib:none

Also, use the reference assembly, not implementation assembly. The implementation assembly is not very suitable for compiling. The path to the refs is C:\Program Files\dotnet\sdk\8.0.101\ref\

Also, use the reference assembly, not implementation assembly. The implementation assembly is not very suitable for compiling. The path to the refs is C:\Program Files\dotnet\sdk\8.0.101\ref\

So .NET SDK is required. I will test with .NET SDK and report back.

Also, use the reference assembly, not implementation assembly. The implementation assembly is not very suitable for compiling. The path to the refs is C:\Program Files\dotnet\sdk\8.0.101\ref\

This command worked:

bflat build-il --stdlib=none -r:"C:\Program Files\dotnet\sdk\8.0.101\ref\mscorlib.dll" -r:"C:\Program Files\dotnet\sdk\8.0.101\ref\netstandard.dll" Program.cs -o Test.exe

Thank you.