Main Content

Build .NET Core Application That Runs onLinuxandmacOS

金宝app支持的平台:Windows®(作者),Linux®(执行),以及macOS(执行)。

This example shows how to create a .NET assembly using theLibrary Compiler并将其集成到可以在Linux或macOS

Prerequisites

  1. Create a new work folder that is visible to the MATLAB®搜索路径。此示例使用C:\Workas the new work folder.

  2. 安装MATLABRuntime在Windows和其他平台上,您打算在其中运行.NET Core应用程序。有关详细信息,请参阅安装and Configure MATLAB Runtime

  3. 对于Linux和macOSplatforms, after installingMATLABRuntime,您需要设置ld_library_pathanddyld_library_path环境变量。有关更多信息,请参阅Set MATLAB Runtime Path for Deployment

  4. 验证您有视觉工作室®和.NET Core 2.0或更高安装。如果已安装了Visual Studio 2017的15.8.2版,则无需单独安装.NET Core 2.0或更高版本。

创建.NET组件

使用Library Compiler应用程序。另外,如果要使用程序化方法从MATLAB命令窗口创建.NET组件,请参见compiler.build.dotNETAssembly

  1. 创建一个名为NEW MATLAB文件mymagic.m在工作文件夹中使用以下代码:

    functionout = mymagic(in)out = magic(in);
  2. TypelibraryCompiler在MATLAB命令行中启动库编译器应用程序。

  3. In theTYPEsection of the toolstrip, select。NET Assembly, and in the导出功能section, click the添加按钮添加文件mymagic.m到项目。

  4. In theLibrary informationsection, name the libraryMyMatrixFunctions

  5. 双击班级Class1and rename it asMyMagic

  6. 使用默认项目名称保存部署项目MyMatrixFunctions

  7. 选择Package创建.NET组件。有关创建文件的信息,请参阅包装MATLAB功能后生成的文件

创建.NET核心应用程序

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

  2. 在命令行,键入:

    dotnet新控制台 - 名称mydotnetCoreApp

    This creates a folder namedMyDotNetCoreAppthat has the following contents:

    • OBJ文件夹

    • mydotnetcoreapp.csproj项目文件

    • program.csC#源文件

  3. 在文本编辑器中打开项目文件。

       exe    netCoreapp2.0      

    添加the following references to the project using thetag:

    • .NET组装文件mymatrixfunctions.dllcreated by the Library Compiler app

    • MWArray.dll,位于\ toolbox \ dotnetbuilder \ bin \ win64 \

    Once you add the references, your project file should resemble the following:

       exe    netCoreApp.2      <参考> c:\ work \ mymatrixfunctions \ for_redistribution_files_only \ mymatrixfunctions.dll  <!<!<! - 由库Compiler App创建的.NET组装的路径 - >   <参考:\ program文件\ matlab \ matlab runtime \ v97 \ toolbox \ dotnetbuilder \ bin \ bin \ win64 \ v4.0 \ mwarray.dll.dll  <! -  path to matlab runtime-runtime-runtime->   
  4. Open the C# source fileprogram.cs并用以下代码替换现有代码:

    program.cs

  5. 在命令行,通过键入构建.NET核心项目:

    dotnet构建myDotnetCoreApp.csproj
  6. At the command line, run your application by typing:

    dotnet run -3

    该应用程序显示一个3x3魔法正方形。

  7. 将项目作为独立部署发布,以在Linux或macOS

    • To publish to Linux, type the following command on a single line:

      dotnet Publish-configuration Release-Framework NetCoreApp2.2--luntime linux-x64-固有的true myDotnetCoreApp.csproj

    • To publish tomacOS, type the following command on a single line:

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

运行.NET核心应用程序UNIX

  1. 复制Release文件夹来自c:\ work \ mydotnetCoreApp \ binon Windows to~/Workon a Linux ormacOS机器。

  2. On the Linux machine, verify that you have installedMATLABRuntime并设置您的库路径环境变量。有关更多信息,请参阅Prerequisites

  3. 打开命令外壳并导航到:

    〜/work/prease/netCoreApp2.2//发布
  4. 通过键入运行.NET Core应用程序:

    。/MyDotNetCoreApp 3
    顺序的魔法平方3 8 1 6 3 5 7 4 9 2魔法正方形作为本机数组:元素(0,0)= 8元素(0,1)= 1元素(0,2)= 6元素(1,0)= 3元素(1,1)= 5元素(1,2)= 7元素(2,0)= 4元素(2,1)= 9元素(2,2)= 2

Related Topics