Use mono to let. NET programs run across platforms

Source: Internet
Author: User
Tags mscorlib

As we all know, Unity3d engine with a strong cross-platform ability and the favor of developers, in the Cross-platform application development gradually become mainstream today, with cross-platform development capabilities for programmers is particularly important. The traditional way to develop for different platforms often gives developers a different approach, and it is difficult to ensure that the application has the same and excellent experience on various platforms, in which case finding a cross-platform development approach will find a way to solve the problem. From the current development environment, the web should be the most likely to become a cross-platform development of the weapon, but for a long time in the Web development front-end and back-end have their own different workflows, although there is now a front-end and back-end gradually integration trend, But 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 that implements it across the platform. Mono is an Open-source project designed to make. NET run on Linux. It can be made by the built-in C # language compiler, CLR runtime, and various class libraries. NET applications run on different platforms, such as Windows, Linux, FreeBSD, and so on. In the business world, Xamarin has realized the great pioneering work of using C # to write Android and iOS apps. When Windows10 was released, Microsoft presented the idea of a common application UWP, where developers could write cross-platform applications using C # directly in the latest Visual Studio. Microsoft has recently acquired Xamarin, a move that will ensure that business projects such as Xamarin will be better integrated with Microsoft's products. While Java and PHP are currently dominant in traditional web development, although the popularity of cloud computing technology, the reduction of server costs may make C # Such a good language more mature. I have always believed that technology is not good or bad, the core of all technical problems is people, so next, we intend to follow the--java pioneer, the first proposed "write, run everywhere" great idea to explore the possibility of the C # program Cross-platform. mono principle of 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), a set of ECMA-defined criteria that defines a language-independent, Cross-platform, runtime environment. This allows developers to develop the software in a variety of high-level languages within the specification definition, and allows the 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-platform, Runtime Environment specification, as long as the application that conforms to this specification can run on different computer architectures, that is, 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 uses system virtual machines on the basis of 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 operating system's native code (Native).

Well, now we're going to answer the first question: Why Mono can cross the platform. We look back. NET program running mechanism can be found to achieve the. NET Cross-platform needs these three key: compilers, CLR and base Class library. Under. NET, we write one of the simplest "Hello world" All need to mscorlib.dll this dynamic link library because the. NET Framework has already provided this for us because the. NET Framework is installed on our computers, which is why the applications that we write can run under Windows. Looking back mono, first of all mono and the CLR is the implementation of the CLI this standard, so we can understand that mono implemented and Microsoft provided us with similar things, because of Microsoft. NET Framework belongs to commercial closed-source products, so mono in addition to implementing the CLR and compilers while implementing a large number of base libraries, and to some extent the version of Mono implemented with the same period. NET version of a certain gap, this use of Unity3d development game friends should be deeply touched. 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 able to run in all environments that implement the CLI standard, and the CLI standard is not specific to the platform or CPU, so long as the Mono runtime can guarantee the operation of the CIL, you can implement the Cross-platform application. We can summarize this part by following this diagram: developing 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 program's reliance on platform features, we choose 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
    {public
        static void Main (string[] args)
        {
            Console.WriteLine ("Hello world!");}}

Because our computer is installed. NET Framework, the program we write is compiled into common intermediate language CIL by the C # compiler and then converted to native Code by the CLR. Typically, the Common Intermediate language (CIL) is stored in the. Il file, but we do not seem to have seen the generation of this file at compile time, because the executable file (. exe) generated here is essentially a common intermediate language (CIL)-like executable. We can verify this by ildasm this tool, which can help us view the IL code, which is usually located in C:\Program Files\Microsoft Sdks\windows\v7.0a\bin. The following is the IL code obtained from this tool:

. method public hidebysig static void  Main (string[] args) cil managed
{
  . entrypoint
  /code size       (0xd)
  . maxstack  8
  il_0000:  nop
  il_0001:  ldstr      "Hello world!"
  il_0006:  call       void [Mscorlib]system.console::writeline (String)
  il_000b:  nop
  il_000c:  ret
} End of Method Mainclass::main

You can see that this code exactly corresponds to the main method in the program we wrote, and the meaning of this code is that you can learn the syntax of the IL code through a search engine. Because what we want to show here is that the executable file (. exe) generated here is not an executable file in nature. Because it depends entirely on the CPU, which is different from our application written directly in C + +, we know that different compilers, such as VC + + under Windows and GCC under Linux, are closely connected to the hardware, so our compiled programs can run directly on their respective platforms, That is, the CPU is aware of these programs. But in. NET this is not the same here, because the file we compiled through the C # compiler, Csc.exe, is actually an IL file that looks like an executable, actually a platform-independent, and CPU-independent.

Then we will feel confused ah, usually we compile the C # program double-click can open ah, haha, now grandly please out. NET program's parent common language Runtime (CLR). The common language runtime is actually the regulator of the program's operation, and the running of the program is entirely determined by the runtime. When we double-click the file, the common language runtime loads it into memory, then the Just-in-time compiler (JIT) recognizes the Il file, and the CPU does the appropriate action.

So we can understand that. NET program across platforms, because IL files are a platform-independent, CPU-independent, Cross-platform file structure, so we just need to implement a common language runtime (CLR) on a different platform to run the same program on different platforms. But in this process, a C # compiler is required to convert C # code to IL code, and then need 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, which are not available on platforms other than Windows, so we also need the underlying class libraries in addition to the C # compiler and the common language runtime. Now we have a clearer understanding of the mono. Yes, mono is actually doing the things that we're talking about. Here bloggers want to talk about Just-in-time compilation (JIT) and static compilation (AOT), both of which can be interpreted as "explanatory" and "compiled", and why Unity3d are having trouble doing hot updates on the iOS platform. This is because the iOS platform takes into account the security ban on JIT Just-in-time compilation, so languages like C # that need to be compiled are nowhere to be used here.

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

MCS D:\ Project Management \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, under mono We use Mono.exe This file to complete this step. We enter the following command at the command line:
Mono D:\ Project Management \csharp\monoapplication\monoapplication\main.exe

We can see that the command line outputs our desired Hello world, which means that the program we are writing is now running in mono, which is actually provided by mono under Windows C # The compiler mcs.exe a compiled IL file double-click is straightforward to run because the CLR is installed on our computer and is built into our computer as part of. Net. So we'll find a problem where our cross-platform is actually a cross-platform platform for compilers, runtimes, and base class libraries, which means that we're running. NET programs in Linux that require mono support. Because I can not install mono in Linux offline, so the verification of running. NET program under Linux needs to wait for the blogger to have time to update again. However, we can imagine that the executable files compiled with the C # compiler are not functioning properly under Linux, as Windows programs typically run under Linux in a virtual machine environment or software such as wine. It is clear that such a Windows program is running in Linux because we have Mono installed under Linux. talk about mono after Cross-platform

OK, so far we have basically cleared up the principle of mono cross-platform. We know that Microsoft's technology system in the development process because of some historical legacy problems. NET program compatibility in different versions of Windows sometimes problems, although Microsoft announced that Windows XP stop maintenance, we write Windows applications can ignore the Windows XP version of support, But because the domestic users do not like the online update patch of this general situation, so if the user to install the program to install first. NET Framework will certainly reduce the user experience, and secondly. NET Framework increases the size of the application installation package, so we need one that we can develop. NET application is able to run safely and stably while leaving Microsoft's technology system, we consider using mono to let. NET programs run out of the. NET Framework.

First, let's talk. NET program is able to 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 use Hello World as an example, we need to use the Mono runtime to parse IL code after compiling the IL code using mono, so if we can write a program to invoke the Mono runtime, we can solve the problem. In this case, the size of the streamlined application installation package is essentially a solution to the dependency of the underlying class library, Because mono implements most of the base class libraries in the. NET Framework, the key to porting. NET applications is the porting of the underlying class libraries, such as the WinForm solution under Linux, which is GTK, which is a very important issue when considering cross-platform. Summary

This paper discusses the principle of mono cross-platform. NET application Cross-platform possibilities and concrete implementations. Cross-platform is a topic that involves a lot of content, my personal understanding of Cross-platform is to write cross-platform code, which means that we need to consider reducing the porting of platform features when writing programs, such as LINQ is a great feature, but this feature leaves Windows and leaves. NET has no way to be guaranteed, so it would be a hassle if you wanted to use LINQ applications across platforms. It is difficult to maintain the same experience between different platforms, as we write a Web program that has a different performance from one browser to another, so the problem of Cross-platform is studied with a learning attitude.

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.