Mono 编译器生成的 .exe 直接从 linux 命令行运行,为什么?
Mono compiler generated .exe runs directly from linux command line , why?
在我的 Arch Linux 机器上,我安装了 Mono C# 编译器 [版本 5.20.1.0]。我今天用 C# 创建了一个简单的 hello world 程序如下:
using System;
public class Program
{
public static int Main()
{
Console.WriteLine("Hello world of C# !");
Console.WriteLine();
return 0;
}
}
我去我的 bash 终端并将上面的程序编译为
$ mcs t.cs
这在工作目录中创建了一个名为 t.exe 的文件。然后我输入:
$ ./t.exe
输出是
Hello world of C# !
看到这个我很惊讶,因为以前不是这样的。您不能从 linux 命令行 直接 执行单声道生成的 .exe 文件。要 运行 上面生成的 t.exe 可执行文件,您必须执行以下操作:
$ mono t.exe
为了进一步调查(并检查文件格式)由单声道编译器生成,我 运行 file 命令,像这样,
$ file t.exe
它产生了以下输出:
t.exe: PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows
我在互联网上搜索过,但无法弄清楚为什么会这样。不是我在抱怨,我很喜欢它。但是 linux 命令行 运行 怎么能像这样直接生成 .net 或 mono 可执行文件呢?发生什么事了??
Linux 中 .NET .exe 文件的 "direct" 执行 (1) 是通过 Linux' binfmt_misc 子系统启用的功能。
引自Wikipedia:
binfmt_misc is a capability of the Linux kernel which allows arbitrary executable file formats to be recognized and passed to certain user space applications, such as emulators and virtual machines.
要启用 "direct" .NET 可执行文件的执行,类似于以下内容的行需要出现在可能 binfmt.d directories 之一的配置文件中。
:CLR:M::MZ::/usr/bin/mono:
此外,作为最后一步,任何应该 "directly" 可执行文件的 .NET .exe 都需要设置其执行标志 (chmod 755 ...
)。
(1)"Direct"这里的执行意味着用户只需要在shell中键入文件名,而binfmt_misc 负责自动执行关联的实际 Linux executable/script(在本例中为 mono
),同时将键入的文件的 name/path 传递到 shell .
在我的 Arch Linux 机器上,我安装了 Mono C# 编译器 [版本 5.20.1.0]。我今天用 C# 创建了一个简单的 hello world 程序如下:
using System;
public class Program
{
public static int Main()
{
Console.WriteLine("Hello world of C# !");
Console.WriteLine();
return 0;
}
}
我去我的 bash 终端并将上面的程序编译为
$ mcs t.cs
这在工作目录中创建了一个名为 t.exe 的文件。然后我输入:
$ ./t.exe
输出是
Hello world of C# !
看到这个我很惊讶,因为以前不是这样的。您不能从 linux 命令行 直接 执行单声道生成的 .exe 文件。要 运行 上面生成的 t.exe 可执行文件,您必须执行以下操作:
$ mono t.exe
为了进一步调查(并检查文件格式)由单声道编译器生成,我 运行 file 命令,像这样,
$ file t.exe
它产生了以下输出:
t.exe: PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows
我在互联网上搜索过,但无法弄清楚为什么会这样。不是我在抱怨,我很喜欢它。但是 linux 命令行 运行 怎么能像这样直接生成 .net 或 mono 可执行文件呢?发生什么事了??
Linux 中 .NET .exe 文件的 "direct" 执行 (1) 是通过 Linux' binfmt_misc 子系统启用的功能。
引自Wikipedia:
binfmt_misc is a capability of the Linux kernel which allows arbitrary executable file formats to be recognized and passed to certain user space applications, such as emulators and virtual machines.
要启用 "direct" .NET 可执行文件的执行,类似于以下内容的行需要出现在可能 binfmt.d directories 之一的配置文件中。
:CLR:M::MZ::/usr/bin/mono:
此外,作为最后一步,任何应该 "directly" 可执行文件的 .NET .exe 都需要设置其执行标志 (chmod 755 ...
)。
(1)"Direct"这里的执行意味着用户只需要在shell中键入文件名,而binfmt_misc 负责自动执行关联的实际 Linux executable/script(在本例中为 mono
),同时将键入的文件的 name/path 传递到 shell .