主要内容

本页采用了机器翻译。点击此处可查看英文原文。

构建在 Linux 和 macOS 上运行的 .NET Core 应用程序

R2026b

支持的平台:Windows®(编写)、Linux®(执行),macOS(执行)。

此示例说明如何使用 .NET 程序集编译器创建 .NET 程序集并将其集成到可以在 Linux 或 macOS 上运行的 .NET Core 应用程序中。

前提条件

  1. 创建一个对 MATLAB® 搜索路径可见的新工作文件夹。此示例使用 C:\Work 作为新工作文件夹。

  2. 在 Windows 以及您计划运行 .NET Core 应用程序的其他平台上安装 MATLAB Runtime。有关详细信息,请参阅下载并安装 MATLAB Runtime。

  3. 对于 Linux 和 macOS 平台,安装 MATLAB Runtime 后,您需要分别设置 LD_LIBRARY_PATH 和 DYLD_LIBRARY_PATH 环境变量。有关详细信息,请参阅为部署设置 MATLAB Runtime 库路径。

  4. 确认您已安装 Visual Studio® 和 .NET Core 2.0 或更高版本。如果您已安装 15.8.2 版 Visual Studio 2017,则无需单独安装 .NET Core 2.0 或更高版本。

创建 .NET 程序集

使用 .NET 程序集编译器将函数打包成 .NET 程序集。或者,如果您想使用编程方法从 MATLAB 命令行窗口创建 .NET 程序集,请参阅 compiler.build.dotNETAssembly。

  1. 在工作文件夹中创建一个名为 mymagic.m 的新 MATLAB 文件,并在其中包含以下代码:

    function out = mymagic(in)
    out = magic(in);
  2. 在 MATLAB 命令行中键入 dotNetAssemblyCompiler 以启动 .NET 程序集编译器。

  3. 在导出的函数部分中,点击添加导出的函数按钮以将文件 mymagic.m 添加到工程中。

  4. 在 .NET 程序集信息部分中,将库命名为 MyMatrixFunctions。

  5. 在编译设置部分中,将类 Class1 重命名为 MyMagic。

  6. 选择编译和打包以创建 .NET 程序集和安装程序。有关所创建的文件的信息,请参阅打包 MATLAB 函数后生成的文件。

创建 .NET Core 应用程序

  1. 在 Windows 中打开命令提示符并导航到文件夹 C:\Work。

  2. 在命令行中,键入:

    dotnet new console --name MyDotNetCoreApp

    这会创建一个名为 MyDotNetCoreApp 的文件夹,其中包含以下内容:

    • obj 文件夹

    • MyDotNetCoreApp.csproj 工程文件

    • Program.cs C# 源文件

  3. 在文本编辑器中打开工程文件。

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.0</TargetFramework>
      </PropertyGroup>
    
    </Project>

    使用 <ItemGroup> 标记将以下引用添加到工程中:

    • 由 .NET 程序集编译器创建的 .NET 程序集文件 MyMatrixFunctions.dll

    • MWArray.dll,位于 <MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<framework_version> 中

    添加引用后,您的工程文件应类似于以下内容:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.2</TargetFramework>
      </PropertyGroup>
    
        <ItemGroup>
        <Reference Include="MyMatrixFunctions">
          <HintPath>C:\work\MyMatrixFunctions\output\MyMatrixFunctions.dll</HintPath>
    	  <!--Path to .NET Assembly created by .NET Assembly Compiler app-->
        </Reference>
        <Reference Include="MWArray">
          <HintPath>C:\Program Files\MATLAB\MATLAB Runtime\R2026b\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll</HintPath>
    	  <!--Path to MWArray.dll in the MATLAB Runtime-->
        </Reference>
      </ItemGroup>
    </Project>
  4. 打开 C# 源文件 Program.cs 并将现有代码替换为以下代码:

    // *******************************************************************************
    //
    // Program.cs
    //
    // This example demonstrates how to use MATLAB .NET Assembly to build a simple 
    // component returning a magic square and how to convert MWNumericArray types
    // to native .NET types.
    //
    // Copyright 2001-2019 The MathWorks, Inc.
    //
    // *******************************************************************************
    
    using System;
    
    using MathWorks.MATLAB.NET.Utility;
    using MathWorks.MATLAB.NET.Arrays;
    
    using MyMatrixFunctions;
    
    namespace MathWorks.Examples.MagicSquare
    {
    	/// <summary>
    	/// The MagicSquareApp class computes a magic square of the user specified size.  
    	/// </summary>
    	/// <remarks>
    	/// args[0] - a positive integer representing the size of the magic square.
    	/// </remarks>
    	class Program
    	  {
          #region MAIN
    
    		  /// <summary>
    		  /// The main entry point for the application.
    		  /// </summary>
    		  [STAThread]
    		  static void Main(string[] args)
    		    {
              MWNumericArray? arraySize= null; 
              MWNumericArray? magicSquare= null;
    
              try
                {
                  // Get user specified command line arguments or set default
                  arraySize= (0 != args.Length) ? Double.Parse(args[0]) : 4;
    
                  // Create the magic square object
                  MyMagic magic= new MyMagic();
    
                  // Compute the magic square and print the result
                  magicSquare= (MWNumericArray)magic.mymagic(arraySize);
    
                  Console.WriteLine("Magic square of order {0}\n\n{1}", arraySize, magicSquare);
    
                  // Convert the magic square array to a two dimensional native double array
                  double[,] nativeArray= (double[,])magicSquare.ToArray(MWArrayComponent.Real);
    
                  Console.WriteLine("\nMagic square as native array:\n");
    
                  // Display the array elements:
                  for (int i= 0; i < (int)arraySize; i++) 
                    for (int j= 0; j < (int)arraySize; j++)
                      Console.WriteLine("Element({0},{1})= {2}", i, j, nativeArray[i,j]);
    
                  Console.ReadLine();  // Wait for user to exit application
                }
    
              catch(Exception exception)
                {
                  Console.WriteLine("Error: {0}", exception);
                }
            }
    
          #endregion
    	  }
      }
  5. 在命令行中,通过键入以下命令来编译您的 .NET Core 工程:

    dotnet build MyDotNetCoreApp.csproj
  6. 在命令行中,通过键入以下命令来运行您的应用程序:

    dotnet run -- 3

    应用程序会显示一个 3×3 幻方。

  7. 将工程发布为自包含部署,以便在 Linux 或 macOS 上执行应用程序。

    • 要发布到 Linux,请在一行中键入以下命令:

      dotnet publish --configuration Release --framework netcoreapp2.2 
        --runtime linux-x64 --self-contained true MyDotNetCoreApp.csproj

    • 要发布到 macOS,请在一行中键入以下命令:

      dotnet publish --configuration Release --framework netcoreapp2.2 
        --runtime osx.10.11-x64 --self-contained true MyDotNetCoreApp.csproj

在 Linux 上运行 .NET Core 应用程序

  1. 将 Release 文件夹从 Windows 上的 C:\Work\MyDotNetCoreApp\bin 复制到 Linux 或 macOS 计算机上的 ~/Work。

  2. 在 Linux 计算机上,验证您是否已安装 MATLAB Runtime 并设置了库路径环境变量。有关详细信息,请参阅前提条件。

  3. 打开命令 shell 并导航到:

    ~/Work/Release/netcoreapp2.2/<os-architecture>/publish
  4. 通过键入以下命令来运行 .NET Core 应用程序:

    ./MyDotNetCoreApp 3
    Magic square of order 3
    
         8     1     6
         3     5     7
         4     9     2
    
    Magic square as native array:
    
    Element(0,0)= 8
    Element(0,1)= 1
    Element(0,2)= 6
    Element(1,0)= 3
    Element(1,1)= 5
    Element(1,2)= 7
    Element(2,0)= 4
    Element(2,1)= 9
    Element(2,2)= 2
    

另请参阅

主题