zoukankan      html  css  js  c++  java
  • 在Mac OS X上用自己编译出的CoreCLR运行.NET程序

    当昨天被Mac OS X上无法编译CoreCLR的问题困扰时(详见Mac OS X上尝试编译CoreCLR源代码),后来发现这个难题竟然被神人@kangaroo给解决了,连CoreCLR的微软开发人员也大吃一惊(详见#105)。

    @kangaroo This is awesome! Thank you so much for doing it! I’ll start reviewing it in the morning.

    今天发现,@kangaroo修改后可在OS X上编译的版本已被合并到CoreCLR的主分支上(详见Merge pull request #117 from kangaroo/osx),直接git签出就能编译了。如果编译时出现"not found for architecture x86_64"的错误,需要删除binaries文件夹之后重新编译。

    编译成功之后,你会在 binaries/Product/amd64/debug 文件夹中看到下面三个文件:

    corerun		libcoreclr.dylib	libmscordaccore.dylib

    紧随编译成功的喜悦,一个冲动油然而生——用这个自己编译出来的CoreCLR运行.NET程序。

    要运行.NET程序时,发现 binaries/Product/amd64/debug 中少了一个重要的文件——mscorlib.dll,不知道编译时为什么没生成。

    @kangaroo也知道了这个问题,将这个文件发给了我(如果你需要,可以在这里下载),于是将这个文件复制到 binaries/Product/amd64/debug 中。

    接下来是准备要运行的.NET程序,突然想到之前在阅读园子里的博文用CIL写程序:你好,沃尔德时,照着手工敲出过一段.NET控制台应用程序的IL代码(代码如下),何不就用这个呢?

    .assembly extern mscorlib
    {
        .ver 4:0:0:0
        .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
    }
    .assembly 'HelloWorld'
    {
        .custom instance void class [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::'.ctor'() = (
            01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78
            63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01       )
    
        .hash algorithm 0x00008004
        .ver  0:0:0:0
    }
    .module HelloWorld.exe
    .method static void Main()
    {
        .entrypoint
        .maxstack 1
    
        ldstr "Hello, world!"
        call void [mscorlib]System.Console::WriteLine(string)
        
        ret
    }

    然后用Mono的ilasm将il代码生成为程序集HelloWorld.exe:

    ilasm HelloWorld.il

    接着将HelloWorld.exe复制到 binaries/Product/amd64/debug 中,现在这个文件夹中就有了5个文件:

    /git/dotnet/coreclr/binaries/Product/amd64/debug
    HelloWorld.exe		corerun			libcoreclr.dylib	libmscordaccore.dylib	mscorlib.dll

    激动人心的紧张时刻到来了,我们来用自己编译出来的CoreCLR运行HelloWorld.exe试试:

    $ ./corerun HelloWorld.exe
    Hello, world!

    耶!成功!

    虽然这只是一小步,虽然实现在Mac上开发.NET应用任重而道远,但是.NET开源带来的精彩已经到来!

  • 相关阅读:
    IssueQuery failed in redmine rake tasks
    rubymine 调试 redmine
    redmine rake tasks
    rails tutorial sample app
    win7 chm 打开失败记录
    rails再体验(第一个程序)
    Bitnami Redmine插件开发记录
    redmine export long csv file failed: 502 proxy error
    Java时区切换时的需要注意
    Android No static field XXX of type I in class Lcom/XXX/R$id错
  • 原文地址:https://www.cnblogs.com/dudu/p/mac-os-x-coreclr-hello-world.html
Copyright © 2011-2022 走看看