简体   繁体   中英

Mono Compilation Error in C#

To start, I copied this code from the Microsoft website:

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.

The real problem starts when i start compiling. First, I try using mcs HelloWorld.cs which turns up this error:

bash: mcs: command not found

when I use mono --aot HelloWorld.cs , I get this error:

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.

How can I get this program to compile?

Debug from trying to install 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. 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. More info on setting the path: Mono Compiler is not working

  2. mono is used to run compiled programs so that won't work, you'll have to compile it first. C# code is first compiled into an intermediate language representation, usually saved as *.dll files which you deploy. These are then compiled again just-in-time (JIT) when you actually run it with the framework on the server. This allows the program to be automatically compatible with the exact CPU architecture it's running on, along with any optimizations necessary. If you want to do this ahead-of-time (AOT), then you can take the compiled output and use the --aot flag.

  3. Why not use .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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