简体   繁体   English

C#中的Mono编译错误

[英]Mono Compilation Error in C#

To start, I copied this code from the Microsoft website: 首先,我从Microsoft网站复制了以下代码:

using System;
namespace HelloWorld
{
    class Hello 
    {
        static void Main() 
        {
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

This is saved as HelloWorld.cs This is all fine and dandy. 这被保存为HelloWorld.cs。很好。

The real problem starts when i start compiling. 真正的问题开始于我开始编译时。 First, I try using mcs HelloWorld.cs which turns up this error: 首先,我尝试使用mcs HelloWorld.cs出现此错误:

bash: mcs: command not found

when I use mono --aot HelloWorld.cs , I get this error: 当我使用mono --aot HelloWorld.cs ,出现以下错误:

Cannot open assembly 'HelloWorld.cs': File does not contain a valid CIL image.

Using a RPI 3 using Raspbian and Mono Runtime v2.0.50727. 使用Raspbian和Mono Runtime v2.0.50727的RPI 3。

How can I get this program to compile? 如何获得该程序进行编译?

Debug from trying to install mono-complete: 从尝试安装mono-complete进行调试:

pi@raspberrypi:~ $ sudo apt-get install mono-complete
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:

    The following packages have unmet dependencies:
     mono-complete : Depends: mono-runtime (= 5.4.1.6-0xamarin1+raspbian9b1) but 3.2.8+dfsg-10 is to be installed
                         Depends: mono-runtime-sgen (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: mono-utils (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: mono-devel (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: mono-mcs (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: mono-roslyn (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: mono-csharp-shell (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: mono-4.0-gac (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: mono-4.0-service (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: monodoc-base (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: monodoc-manual (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: libmono-cil-dev (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
                         Depends: ca-certificates-mono (= 5.4.1.6-0xamarin1+raspbian9b1) but it is not going to be installed
     mono-runtime : Depends: mono-runtime-sgen (= 3.2.8+dfsg-10) but it is not going to be installed
    E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.
  1. The mcs command is not found, as the error states. 找不到mcs命令,因为错误状态。 Most likely it's either not installed or your environment PATH doesn't include the folder where it is located to be able to find and run it. 它很可能没有安装,或者您的环境PATH不包含它所在的文件夹,以便能够查找和运行它。 More info on setting the path: Mono Compiler is not working 有关设置路径的更多信息: Mono编译器不起作用

  2. mono is used to run compiled programs so that won't work, you'll have to compile it first. mono用于运行已编译的程序,因此它将无法正常工作,您必须先对其进行编译。 C# code is first compiled into an intermediate language representation, usually saved as *.dll files which you deploy. 首先将C#代码编译为中间语言表示形式,通常将其保存为您部署的*.dll文件。 These are then compiled again just-in-time (JIT) when you actually run it with the framework on the server. 当您使用服务器上的框架实际运行它们时,它们将再次进行实时编译(JIT)。 This allows the program to be automatically compatible with the exact CPU architecture it's running on, along with any optimizations necessary. 这使程序可以自动与其运行的确切CPU架构兼容,并进行必要的优化。 If you want to do this ahead-of-time (AOT), then you can take the compiled output and use the --aot flag. 如果要提前进行此操作(AOT),则可以获取编译后的输出并使用--aot标志。

  3. Why not use .NET Core? 为什么不使用.NET Core? It is the latest cross-platform framework and should run just fine on your Raspberry Pi, use these instructions: https://github.com/dotnet/core/blob/master/samples/RaspberryPiInstructions.md 这是最新的跨平台框架,应在您的Raspberry Pi上正常运行,请使用以下说明: https : //github.com/dotnet/core/blob/master/samples/RaspberryPiInstructions.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM