你的位置:首页 > ASP.net教程

[ASP.net教程].netcore的跨平台 之 在windows上编译,并在ubuntu上运行


1 下载并安装netcore sdk
    下载地址 https://github.com/dotnet/cli  


                       

      选取合适的版本下载安装即可

     打开 CMD ,输入dotnet,出现以下信息说明已安装好

C:\Users\71700>dotnetUsage: dotnet [--help | app.dll]

 2 新建一个项目 

新建项目在cmd 里输入dotnet new 即可

 

C:\Users\71700>mkdir demoC:\Users\71700>cd demoC:\Users\71700\demo>dotnet newCreated new C# project in C:\Users\71700\demo.C:\Users\71700\demo>dir 驱动器 C 中的卷没有标签。 卷的序列号是 3A94-0B71 C:\Users\71700\demo 的目录2016-04-25 14:58  <DIR>     .2016-04-25 14:58  <DIR>     ..2016-04-25 14:58        214 Program.cs2016-04-25 14:58        308 project.json        2 个文件      522 字节        2 个目录 87,409,934,336 可用字节

 

可以看到共生成了两个文件  program.cs和project.json 以下是内容
 Program.cs

using System;namespace ConsoleApplication{  public class Program  {    public static void Main(string[] args)    {      Console.WriteLine("Hello World!");    }  }}

project.json

{ "version": "1.0.0-*", "compilationOptions": {  "emitEntryPoint": true }, "dependencies": {  "Microsoft.NETCore.App": {   "type": "platform",   "version": "1.0.0-rc2-3002485"  } }, "frameworks": {  "netcoreapp1.0": {   "imports": "dnxcore50"  } }}

很简单的两个文件 ,就是要输出hello world

3 修改project.json 来支持交叉编译

{ "version": "1.0.0-*", "compilationOptions": {  "emitEntryPoint": true }, "dependencies": {  "Microsoft.NETCore.App": {   "type": "platform",   //此处去掉,加上的话发布的时候不会netcore的运行环境和基础类库一块发布,需要在目标平台上安装netcore ,去掉的话就不用在目标平台上安装netcore了   
    "version": "1.0.0-rc2-3002485" } }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } },
  "runtimes":{"ubuntu.15.04-x64":{}} //加入runtimes
}

 

 

4保存文件,还原引用包 运行dotnet restore

C:\Users\71700\demo>dotnet restorelog : Restoring packages for C:\Users\71700\demo\project.json...info :  GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.jsoninfo :  CACHE https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/microsoft.netcore.dotnethostresolver/index.jsoninfo :  NotFound https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json 1208msinfo :  CACHE https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/microsoft.netcore.dotnethost/index.jsoninfo :  GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.jsoninfo :  NotFound https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json 1935msinfo : Committing restore...log : Writing lock file to disk. Path: C:\Users\71700\demo\project.lock.jsonlog : C:\Users\71700\demo\project.jsonlog : Restore completed in 7649ms.NuGet Config files used:  C:\Users\71700\NuGet.Config  C:\Users\71700\AppData\Roaming\NuGet\NuGet.ConfigFeeds used:  https://www.myget.org/F/dotnet-core/api/v3/index.json  https://api.nuget.org/v3/index.json

5 编译代码 dotnet build -r ubuntu.15.04-x64

C:\Users\71700\demo>dotnet build -r ubuntu.15.04-x64
Compiling demo for .NETCoreApp,Version=v1.0

Compilation succeeded.
0 Warning(s)
0 Error(s)

Time elapsed 00:00:01.4677989


6发布程序 dotnet publish -r ubuntu.15.04-x64

C:\Users\71700\demo>dotnet publish -r ubuntu.15.04-x64Publishing demo for .NETCoreApp,Version=v1.0/ubuntu.15.04-x64Project demo (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.publish: Published to C:\Users\71700\demo\bin\Debug\netcoreapp1.0\ubuntu.15.04-x64\publishPublished 1/1 projects successfully

查看下publish文件夹

文件很多,其中demo是ubuntu上的可执行文件

7把publish文件加拷贝的共享文件夹里面 并挂载到ubuntu上 运行demo
 先ls查看下文件里面的内容

root@lius-Virtual-Machine:/mnt/share/publish# lsdemo                   System.Net.Http.dlldemo.deps.json              System.Net.Http.Native.sodemo.dll                 System.Net.NameResolution.dlldemo.pdb                 System.Net.Primitives.dlldemo.runtimeconfig.json         System.Net.Requests.dlllibcoreclr.so              System.Net.Security.dlllibcoreclrtraceptprovider.so       System.Net.Security.Native.solibdbgshim.so              System.Net.Sockets.dlllibhostfxr.so              System.Net.WebHeaderCollection.dlllibhostpolicy.so             System.Numerics.Vectors.dlllibmscordaccore.so            System.ObjectModel.dlllibmscordbi.so              System.Private.Uri.dlllibsosplugin.so             System.Reflection.DispatchProxy.dlllibsos.so                System.Reflection.dllMicrosoft.CodeAnalysis.CSharp.dll    System.Reflection.Emit.dllMicrosoft.CodeAnalysis.dll        System.Reflection.Emit.ILGeneration.dllMicrosoft.CodeAnalysis.VisualBasic.dll  System.Reflection.Emit.Lightweight.dllMicrosoft.CSharp.dll           System.Reflection.Extensions.dllMicrosoft.VisualBasic.dll        System.Reflection.Metadata.dllMicrosoft.Win32.Primitives.dll      System.Reflection.Primitives.dllMicrosoft.Win32.Registry.dll       System.Reflection.TypeExtensions.dllmscorlib.dll               System.Resources.Reader.dllmscorlib.ni.dll             System.Resources.ResourceManager.dllsosdocsunix.txt             System.Runtime.dllSystem.AppContext.dll          System.Runtime.Extensions.dllSystem.Buffers.dll            System.Runtime.Handles.dllSystem.Collections.Concurrent.dll    System.Runtime.InteropServices.dllSystem.Collections.dll          System.Runtime.InteropServices.PInvoke.dllSystem.Collections.Immutable.dll     System.Runtime.InteropServices.RuntimeInformation.dllSystem.Collections.NonGeneric.dll    System.Runtime.Loader.dllSystem.Collections.Specialized.dll    System.Runtime.Numerics.dllSystem.ComponentModel.Annotations.dll  System.Security.Claims.dllSystem.ComponentModel.dll        System.Security.Cryptography.Algorithms.dllSystem.Console.dll            System.Security.Cryptography.Cng.dllSystem.Diagnostics.Debug.dll       System.Security.Cryptography.Csp.dllSystem.Diagnostics.DiagnosticSource.dll System.Security.Cryptography.Encoding.dllSystem.Diagnostics.FileVersionInfo.dll  System.Security.Cryptography.Native.soSystem.Diagnostics.Process.dll      System.Security.Cryptography.OpenSsl.dllSystem.Diagnostics.StackTrace.dll    System.Security.Cryptography.Primitives.dllSystem.Diagnostics.Tools.dll       System.Security.Cryptography.X509Certificates.dllSystem.Diagnostics.Tracing.dll      System.Security.Principal.dllSystem.Dynamic.Runtime.dll        System.Security.Principal.Windows.dllSystem.Globalization.Calendars.dll    System.Text.Encoding.CodePages.dllSystem.Globalization.dll         System.Text.Encoding.dllSystem.Globalization.Extensions.dll   System.Text.Encoding.Extensions.dllSystem.Globalization.Native.so      System.Text.RegularExpressions.dllSystem.IO.Compression.dll        System.Threading.dllSystem.IO.Compression.Native.so     System.Threading.Overlapped.dllSystem.IO.Compression.ZipFile.dll    System.Threading.Tasks.Dataflow.dllSystem.IO.dll              System.Threading.Tasks.dllSystem.IO.FileSystem.dll         System.Threading.Tasks.Extensions.dllSystem.IO.FileSystem.Primitives.dll   System.Threading.Tasks.Parallel.dllSystem.IO.FileSystem.Watcher.dll     System.Threading.Thread.dllSystem.IO.MemoryMappedFiles.dll     System.Threading.ThreadPool.dllSystem.IO.UnmanagedMemoryStream.dll   System.Threading.Timer.dllSystem.Linq.dll             System.

执行dome

root@lius-Virtual-Machine:/mnt/share/publish# ./demoHello World!

成功输出 helloworld