.netcore跨平臺 之 windows上編譯,ubuntu上運行

1 下載並安裝netcore sdk
    下載地址 https://github.com/dotnet/cli  git


                       

      選取合適的版本下載安裝便可github

     打開 CMD ,輸入dotnet,出現如下信息說明已安裝好json

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

 2 新建一個項目 ubuntu

新建項目在cmd 裏輸入dotnet new 便可windows

 

C:\Users\71700>mkdir demo

C:\Users\71700>cd demo

C:\Users\71700\demo>dotnet new
Created 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.cs
2016-04-25  14:58               308 project.json
               2 個文件            522 字節
               2 個目錄 87,409,934,336 可用字節

 

能夠看到共生成了兩個文件  program.cs和project.json 如下是內容
 Program.csapi

using System;

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

project.jsonapp

{
  "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

dom

3 修改project.json 來支持交叉編譯ide

{
  "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 restoreui

C:\Users\71700\demo>dotnet restore
log  : Restoring packages for C:\Users\71700\demo\project.json...
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json
info :   CACHE https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/microsoft.netcore.dotnethostresolver/index.json
info :   NotFound https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json 1208ms
info :   CACHE https://dotnetmyget.blob.core.windows.net/artifacts/dotnet-core/nuget/v3/flatcontainer/microsoft.netcore.dotnethost/index.json
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json
info :   NotFound https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json 1935ms
info : Committing restore...
log  : Writing lock file to disk. Path: C:\Users\71700\demo\project.lock.json
log  : C:\Users\71700\demo\project.json
log  : Restore completed in 7649ms.

NuGet Config files used:
    C:\Users\71700\NuGet.Config
    C:\Users\71700\AppData\Roaming\NuGet\NuGet.Config

Feeds 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-x64
Publishing demo for .NETCoreApp,Version=v1.0/ubuntu.15.04-x64
Project 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\publish
Published 1/1 projects successfully

查看下publish文件夾

文件不少,其中demo是ubuntu上的可執行文件

7把publish文件加拷貝的共享文件夾裏面 並掛載到ubuntu上 運行demo
 先ls查看下文件裏面的內容

root@lius-Virtual-Machine:/mnt/share/publish# ls
demo                                     System.Net.Http.dll
demo.deps.json                           System.Net.Http.Native.so
demo.dll                                 System.Net.NameResolution.dll
demo.pdb                                 System.Net.Primitives.dll
demo.runtimeconfig.json                  System.Net.Requests.dll
libcoreclr.so                            System.Net.Security.dll
libcoreclrtraceptprovider.so             System.Net.Security.Native.so
libdbgshim.so                            System.Net.Sockets.dll
libhostfxr.so                            System.Net.WebHeaderCollection.dll
libhostpolicy.so                         System.Numerics.Vectors.dll
libmscordaccore.so                       System.ObjectModel.dll
libmscordbi.so                           System.Private.Uri.dll
libsosplugin.so                          System.Reflection.DispatchProxy.dll
libsos.so                                System.Reflection.dll
Microsoft.CodeAnalysis.CSharp.dll        System.Reflection.Emit.dll
Microsoft.CodeAnalysis.dll               System.Reflection.Emit.ILGeneration.dll
Microsoft.CodeAnalysis.VisualBasic.dll   System.Reflection.Emit.Lightweight.dll
Microsoft.CSharp.dll                     System.Reflection.Extensions.dll
Microsoft.VisualBasic.dll                System.Reflection.Metadata.dll
Microsoft.Win32.Primitives.dll           System.Reflection.Primitives.dll
Microsoft.Win32.Registry.dll             System.Reflection.TypeExtensions.dll
mscorlib.dll                             System.Resources.Reader.dll
mscorlib.ni.dll                          System.Resources.ResourceManager.dll
sosdocsunix.txt                          System.Runtime.dll
System.AppContext.dll                    System.Runtime.Extensions.dll
System.Buffers.dll                       System.Runtime.Handles.dll
System.Collections.Concurrent.dll        System.Runtime.InteropServices.dll
System.Collections.dll                   System.Runtime.InteropServices.PInvoke.dll
System.Collections.Immutable.dll         System.Runtime.InteropServices.RuntimeInformation.dll
System.Collections.NonGeneric.dll        System.Runtime.Loader.dll
System.Collections.Specialized.dll       System.Runtime.Numerics.dll
System.ComponentModel.Annotations.dll    System.Security.Claims.dll
System.ComponentModel.dll                System.Security.Cryptography.Algorithms.dll
System.Console.dll                       System.Security.Cryptography.Cng.dll
System.Diagnostics.Debug.dll             System.Security.Cryptography.Csp.dll
System.Diagnostics.DiagnosticSource.dll  System.Security.Cryptography.Encoding.dll
System.Diagnostics.FileVersionInfo.dll   System.Security.Cryptography.Native.so
System.Diagnostics.Process.dll           System.Security.Cryptography.OpenSsl.dll
System.Diagnostics.StackTrace.dll        System.Security.Cryptography.Primitives.dll
System.Diagnostics.Tools.dll             System.Security.Cryptography.X509Certificates.dll
System.Diagnostics.Tracing.dll           System.Security.Principal.dll
System.Dynamic.Runtime.dll               System.Security.Principal.Windows.dll
System.Globalization.Calendars.dll       System.Text.Encoding.CodePages.dll
System.Globalization.dll                 System.Text.Encoding.dll
System.Globalization.Extensions.dll      System.Text.Encoding.Extensions.dll
System.Globalization.Native.so           System.Text.RegularExpressions.dll
System.IO.Compression.dll                System.Threading.dll
System.IO.Compression.Native.so          System.Threading.Overlapped.dll
System.IO.Compression.ZipFile.dll        System.Threading.Tasks.Dataflow.dll
System.IO.dll                            System.Threading.Tasks.dll
System.IO.FileSystem.dll                 System.Threading.Tasks.Extensions.dll
System.IO.FileSystem.Primitives.dll      System.Threading.Tasks.Parallel.dll
System.IO.FileSystem.Watcher.dll         System.Threading.Thread.dll
System.IO.MemoryMappedFiles.dll          System.Threading.ThreadPool.dll
System.IO.UnmanagedMemoryStream.dll      System.Threading.Timer.dll
System.Linq.dll                          System.Xml.ReaderWriter.dll
System.Linq.Expressions.dll              System.Xml.XDocument.dll
System.Linq.Parallel.dll                 System.Xml.XmlDocument.dll
System.Linq.Queryable.dll                System.Xml.XPath.dll
System.Native.a                          System.Xml.XPath.XDocument.dll
System.Native.so

執行dome

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

成功輸出 helloworld

相關文章
相關標籤/搜索