.Net Native 跨平台尝试

微软平台的开源愈演愈烈,在前两天的Connect()上,不仅发布了最新的.Net Core RC1,而且进一步开源了.Net Native跨平台实现。

.Net Native 运行时:

https://github.com/dotnet/corert

.NET 命令行工具链:

https://github.com/dotnet/cli

.Net Native提供了普通模式和cpp模式:

普通模式 类似现有的uwp .Net Native,将IL直接编译成机器码然后与运行库链接。

cpp模式 将IL编译成c++源码,然后使用c++编译器编译,类似于unity3d的IL2CPP,这种方式将具有很好的跨平台前景。

以下是Linux Ubuntu 14.04尝试过程(Windows下失败):

安装方法mono

https://github.com/dotnet/corert/blob/master/Documentation/prerequisites-for-building.md

安装必要工具

apt-get update
apt-get -y install curl libicu-dev libunwind8 gettext libssl-dev libcurl4-openssl-dev zlib1g
apt-get -y install autoconf automake build-essential libtool

在cli项目中提供了deb安装包与tar.gz两种格式,我下载deb安装并修复依赖

mkdir dotnetcli
cd dotnetcli
wget https://dotnetcli.blob.core.windows.net/dotnet/dev/Installers/Latest/dotnet-linux-x64.latest.deb
dpkg -i dotnet-linux-x64.latest.deb
apt-get -y -f install

然后设置环境变量(如需重新启动可用,可以写入/etc/profile文件)

export DOTNET_HOME=/usr/share/dotnet

输入 dotnet 可以看到以下输出表示安装成功

.NET Command Line Interface
Usage: dotnet [common-options] [command] [arguments]

Arguments:
  [command]     The command to execute
  [arguments]   Arguments to pass to the command

Common Options (passed before the command):
  -v|--verbose  Enable verbose output

Common Commands:
  compile       Compiles a .NET project
  publish       Publishes a .NET project for deployment
  run           Compiles and immediately executes a .NET project

使用git 下载示例代码

cd /root
git clone https://github.com/dotnet/core.git

先恢复nuget包

cd /root/core/samples/helloworld
dotnet restore

使用.Net Native 普通模式编译

dotnet compile --native

使用.Net Native cpp模式编译

dotnet compile --native --cpp

没有错误则代表成功,可以运行测试

cd /root/core/samples/helloworld/bin/Debug/dnxcore50/native
ls
./helloworld

可以看到成功输出

Hello World!

注意:当前.Net Native仅可以运行samples下的helloworld和dotnetbot,不过相信很快就可以全面使用,因为开发已接近尾声,现在问题在工具之前的配合

原文地址:https://www.cnblogs.com/loqi/p/4980272.html