Using mono to run a. NET program across platforms

Source: Internet
Author: User
Tags mscorlib

?? As we all know, the Unity3d engine with a strong cross-platform capabilities and the favor of developers, in cross-platform application development is becoming mainstream today, with cross-platform development capabilities for programmers is particularly important. The traditional way of developing for different platforms often makes developers forgotten how, making it difficult to ensure that applications have the same, great experience on different platforms, and finding a way to cross-platform development will find an idea for solving this problem. From the current development environment, the web should be the most likely to become a cross-platform development of the divine weapon, but for a long time in web development in the front and back end have their own different workflows, although there are now the front end and the back end of the gradual convergence of the trend, The blogger seems to want to make web development as simple as traditional development requires a certain transition period.

From Mono to Xamarin

?? For Unity3d, Mono is the core technology to achieve its cross-platform. Mono is an open source project designed to make. NET run on Linux. It can be made through the built-in C # language compiler, CLR runtime, and various class libraries. NET applications run on different platforms such as Windows, Linux, and FreeBSD. In the business world, Xamarin has realized the great feat of writing Android and iOS apps in C #. When Windows10 was released, Microsoft put forward the idea of a generic app UWP, where developers could write cross-platform applications directly in the latest Visual Studio using C #. Microsoft recently acquired Xamarin, a move that would ensure that a business project like Xamarin could be better integrated with Microsoft's products. While Java and PHP are currently dominant in traditional web development, while the popularity of cloud computing technology, the reduced cost of the server may make C # Such a good language more mature. I have always believed that technology is not the difference between good and bad, the core of all technical problems is people, so next, we intend to follow the development of cross-platform pioneer--java, the first proposed "write, run everywhere" great idea to explore the possibility of C # program cross-platform.

The principle of mono cross-platform

?? When it comes to mono cross-platform, we first need to introduce the concept of the Common Language Foundation (Common Language infrastructure,cli), the CLI is a set of ECMA-defined standards that defines a language-independent, cross-architecture operating environment, This allows developers to develop software in a variety of high-level languages within the specification definition, and allows software to run on different computer architectures without modification. So we can say that the cross-platform principle is because we define a language-independent, cross-architecture operating environment specification, as long as the application that conforms to this specification can run on different computer architectures, that is, implementing cross-platform. For this standard, Microsoft implements the common language runtime (Common Language runtime,clr), so the CLR is an implementation of the CLI. We are familiar with. NET Framework is a programming platform that employs a system virtual machine on the CLR, which provides us with support for a variety of programming languages such as C #, vb.net, C + +, Python, and so on. The C # program that we write is first compiled by the C # compiler into a common intermediate language, CIL or MSIL (Microsoft intermediate Language), and then converted by the CLR to the native code of the operating system (Native).

?? OK, now let's answer the first question: Why Mono can cross the platform. We look back. NET program running mechanism can be found to implement. NET cross-platform actually need these three key: compiler, CLR and base Class library. In. NET we write the simplest "Hello world" Need to mscorlib.dll this dynamic link library because the. NET Framework already provides this for us because the. NET Framework is installed on our computers, which is why the applications we write are able to run under Windows. Looking back at Mono, first mono and the CLR are all implementations of the CLI standard, so we can understand that mono implements the similar stuff that Microsoft provides to us because of Microsoft's. NET Framework belongs to the commercialization of closed-source products, so in addition to implementing the CLR and compilers to implement a large number of base libraries, and to some extent, the version of Mono implemented with the same period. NET version has a certain gap, this use Unity3d development game friends should have deep feelings! This determines whether we can achieve the same experience on the target platform and on the current platform when porting the application to the target platform. Because the Common intermediate language, CIL, is capable of running in all environments that implement the CLI standard, and the CLI standard is independent of the specific platform or CPU, it is possible to implement cross-platform applications as long as the Mono runtime guarantees CIL operation. We can summarize this section by following this diagram:

Development of the first cross-platform program

?? Let's try to develop the first cross-platform program, we use Visual Studio or MonoDevelop to write a simple console application, in order to reduce the application of the platform to rely on the features, we choose the system this namespace to achieve the most basic hello World, which means that our application does not use any libraries other than mscorlib.dll:

using System;namespace MonoApplication{    class MainClass    {        publicstaticvoid Main(string[] args)        {            Console.WriteLine("Hello World!");        }    }}

?? Because our computer is installed. NET Framework, so the program that we write is compiled by the C # compiler into a common intermediate language CIL, and then converted by the CLR to native Code. Generally, Common Intermediate language (CIL) is stored in the. Il file, but here we do not seem to see the creation of this file at compile time, because the resulting executable (. exe) is essentially an executable file of the Common Intermediate language (CIL) pattern. This can be verified by ildasm This tool, which helps us to look at the IL code, which is usually located in C:\Program Files\Microsoft Sdks\windows\v7.0a\bin. The following is the IL code obtained through this tool:

.method public  hidebysig static  void  main  ( Span class= "Hljs-keyword" >string  [] args) CIL managed{. entrypoint //code size 0x d) . Maxstack 8  il_0000:nop il_0001:ldstr  "H  Ello world! " Il_0006:call void  [Mscorlib]system.console::writeline (string ) Il_000b:nop Il_000c:ret} //End of method Mainclass::main   /pre>

?? You can see that this code is exactly the same as the main method in the program we are writing, and we can use the search engine to understand the syntax of the IL code for the meaning of this piece of code. Because what we want to illustrate here is that the executable file (. exe) generated here is not an executable file in nature. Because it can be executed completely depends on the CPU, this and we write directly in C + + applications, we know that different compilers such as VC + + and Linux under the GCC are closely related to the hardware, so we compiled the program can be run directly on the respective platform, That is, the CPU is aware of these programs. But in. NET here is not the same, because we compiled the C # compiler, Csc.exe, is actually a file that looks like an executable file, but is actually a peace platform independent, and CPU-independent IL file.

?? Then we will feel confused ah, usually we compile the C # program double-click can open ah, haha, now solemnly please out. NET program's parent common language Runtime (CLR). The common language runtime is actually the supervisor of the program, and the running of the program is determined entirely by the runtime. When we double-click the file, the common language runtime loads it into memory, and the Il file is recognized by the instant compiler (JIT), which is then done by the CPU.

?? So we can understand that. NET program across platforms, because IL files are a platform-independent, CPU-independent, cross-platforms file structure, so we just need to implement a common language runtime (CLR) on different platforms to run the same program on different platforms. But in this process, there is a need for a C # compiler to convert C # code to IL code, and then need to have a common language runtime (CLR) to parse the IL code. At the same time, we are. NET Framework uses a large number of base class libraries that are not available on platforms other than Windows, so we need a base class library in addition to the C # compiler and the common language runtime. Now, do you have a clearer understanding of mono? Yes, what mono does is actually what we're talking about. Bloggers here want to talk about instant-compile (JIT) and static compilation (AOT), both of which we can follow in terms of "interpreted" and "compiled" to understand why Unity3d is having problems with hot updates on the iOS platform. This is because the iOS platform takes into account that security prohibits JIT-on-the-fly compilation, so a language like C # needs to be compiled here.

?? Well, since we have a tool like mono that can help us achieve cross-platform development. So now we're going to consider porting this program to the Linux platform, where Linux deepin, for example, we follow the C # program compilation process to complete the porting process:
* 1, the C # program compiled into IL file: under. NET we use csc.exe this program to complete the compilation, in mono we use Mcs.exe this program to complete the compilation, this program after the installation of mono in its installation directory can be found. We enter the command at the command line:

mcs D:\项目管理\CSharp\MonoApplication\MonoApplication\Main.cs
    • 2, this will generate Main.exe such an Il file, now we need a runtime to parse it, in. NET we use the CLR to complete this step, in mono we use Mono.exe This file to complete this step. We enter the following command at the command line:
mono D:\项目管理\CSharp\MonoApplication\MonoApplication\Main.exe

We can see that the command line output our desired Hello world, which means that the program we are writing is now running in mono, and in fact the C # provided by Mono under Windows The compiler mcs.exe compiled IL file double-click can be run directly, because we have the CLR installed on our computer, it is built into our computer as part of. Net. So we'll find a problem where our cross-platform is actually a cross-platform of the three parts of the compiler, runtime, and base class library, which means that we need mono to support running. NET programs under Linux. Because here I can't install mono offline on Linux, so the verification of running. NET programs under Linux requires that bloggers have time to update later! However, we can imagine that the executable file compiled by the C # compiler will not work properly under Linux, because the Windows program normally runs under Linux in a virtual machine environment or software like wine. Obviously letting such a Windows program run in a Linux environment is because we have mono installed under Linux.

About Mono Cross-platform

?? Well, so far we have basically cleared the principle of mono cross-platform. We know that Microsoft's technology system is in the process of development due to some legacy issues. NET programs are sometimes problematic in different versions of Windows, although Microsoft announces Windows XP to stop maintenance, we can ignore support for Windows XP when we write Windows applications. However, because domestic users do not like the general status of online update patches, so if you let the user install the program to install the first. NET Framework is bound to degrade the user experience, and second. NET Framework increases the size of the application installation package, so we need a way that we can develop. NET applications can run safely and stably while departing from Microsoft's technology system, we are here to consider using mono to let. NET programs run out of the. NET Framework.

?? First of all, let's say. NET program can run out of the. NET Framework, we note that Mono provides a mono runtime, so we can run the compiler-generated IL code with such a runtime. We continue to take Hello world as an example, we need to use the Mono runtime to parse IL code after using mono to compile IL code, so if we can write a program to invoke the Mono runtime we can solve the problem. In this case, the size of the thin application installation package is essentially to solve the dependency problem of the base Class library, Because mono implements most of the underlying class libraries in the. NET Framework, the key to porting. NET applications is the porting of the underlying class libraries, such as WinForm's solution under Linux is GTK, which is a very important issue when considering cross-platform scenarios.

Summary

?? This paper discusses the principle of mono cross-platform. NET application cross-platform possibilities and specific implementations. Cross-platform is a topic with a lot of content, I personally understand the cross-platform is to write cross-platform code, which means that we need to write a program to reduce the porting of platform features, such as LINQ is a very good feature, but this feature left Windows, and left. NET has no way to be guaranteed, so it would be a hassle if you were to cross-platform an application that uses LINQ! It's hard to keep the same experience between different platforms, as we write a Web program that behaves differently across browsers, so we're going to look at the problem of cross-platform with a learning attitude.

Using mono to run a. NET program across platforms

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.