700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 创建在Linux上运行的 .NET Core 应用程序(matlab封装的dll文件)

创建在Linux上运行的 .NET Core 应用程序(matlab封装的dll文件)

时间:2021-02-28 04:55:27

相关推荐

创建在Linux上运行的 .NET Core 应用程序(matlab封装的dll文件)

matlab社区:/help/releases/Rb/compiler_sdk/dotnet/create-a-dotnet-core-application-that-runs-on-linux-and-macos.html#mw_bc415a68-a610-472d-b98e-ff757500f504

创建 .NET 程序集

1、创建需要打包的函数,此处以魔方矩阵函数为例:

function out = mymagic(in)out = magic(in);

2、libraryCompiler在 MATLAB 命令行中键入以启动 Library Compiler 应用程序。

3、在工具条的TYPE部分中,选择 .NET Assembly,然后在EXPORTED FUNCTIONS部分中单击Add按钮将文件添加mymagic.m到项目中。

4、在库信息部分,将库命名为 MyMatrixFunctions。将默认类名从 更改 Class1为MyMagic。

5、使用默认项目名称保存部署项目 MyMatrixFunctions。

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

创建 .NET Core 应用程序

1、直接搜powershell,进入命令编辑器,并导航到文件夹C:\work(可以根据自己需要放在那个文件夹下面都行,我是放在C:\work)。

cd C:\work

2、在命令行中,键入:

dotnet new console --name MyDotNetCoreApp

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

obj 文件夹

MyDotNetCoreApp.csproj 项目文件

Program.cs C#源文件

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

里面的内容如下:

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

使用标签向项目添加以下引用 :

MyMatrixFunctions.dll由库编译器应用程序创建的.NET 程序集文件。

C:\work\MyMatrixFunctions\for_redistribution_files_only\MyMatrixFunctions.dll为MyMatrixFunctions.dll的地址,你的地址可能和我的地址不一样,注意要改一下

MWArray.dll是将c#的数据格式转译成MATLAB能读的文件,这个文件可以在你安装MATLAB的路径里面找,我的是在D:\ProgramFiles\MATLABRa\toolbox\dotnetbuilder\bin\win64\v4.0中,应该都可以在安装路径的“toolbox\dotnetbuilder\bin\win64\v4.0”中找到,

添加引用后,项目文件如下所示:

<Project Sdk=".Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>netcoreapp3.1</TargetFramework></PropertyGroup><ItemGroup><Reference Include="MyMatrixFunctions"><HintPath>C:\work\MyMatrixFunctions\for_redistribution_files_only\MyMatrixFunctions.dll</HintPath><!--Path to .NET Assembly created by Library Compiler app--></Reference><Reference Include="MWArray"><HintPath>C:\Program Files\MATLAB\MATLAB Runtime\v97\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- The MathWorks, Inc.//// *******************************************************************************using System;using .Utility;using .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 defaultarraySize= (0 != args.Length) ? Double.Parse(args[0]) : 4;// Create the magic square objectMyMagic magic= new MyMagic();// Compute the magic square and print the resultmagicSquare= (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 arraydouble[,] 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、在命令 shell 中,键入以下内容来构建 .NET Core 项目:

dotnet build MyDotNetCoreApp.csproj

6、在命令 shell 中,通过键入以下内容来运行您的应用程序:

dotnet run -- 3

这将显示一个 3x3 魔方。

7、将项目作为独立部署发布,以便在 Linux 或macOS上执行应用程序。此示例发布到 Linux。在命令 shell 中,键入:

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

要发布到macOS,请键入:

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

这一步创建完成之后就会多出一个bin文件夹。

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

1、将Release文件夹从 C:\work\MyDotNetCoreApp\bin复制到Linux 机器上。文件复制方法有很多种,根据自己需要自行查找和安装。

2、在 Linux 机器上,验证您是否已安装MATLAB Runtime并设置您的LD_LIBRARY_PATH环境变量。

如果没有安装可以查看我写的上一篇文章。Ubentu(20.04)安装Matlab Compiler Runtime(a),centos系统安装也是一样的步骤。

3、打开命令编辑器并导航到:

~/Work/Release/netcoreapp3.1/linux-x64/publish

根据自己文件放入的位置,导航到对应的位置。

4、通过键入以下内容运行 .NET Core 应用程序:

./MyDotNetCoreApp 3如果linux系统报权限不够时,可以运行sudo chmod 777 ./MyDotNetCoreApp再运行./MyDotNetCoreApp 3

输出

3阶魔方举证

8 1 6

3 5 7

4 9 2

作为原生数组的幻方:

Element(0,0)= 8

Element(0,1)= 1

Element(0,2)= 6

Element(1,0) = 3

元素(1,1)= 5

元素(1,2)= 7

元素(2,0)= 4

元素(2,1)= 9

元素(2,2)= 2

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。