Build .NET 5.0 Application That Runs on Linux and macOS
Supported Platform: Windows® (Authoring), Linux® (Execution), and macOS (Execution).
This example shows how to create a .NET assembly using the Library Compiler and integrate it into a .NET 5.0 application that can run on Linux or macOS.
Prerequisites
Create a new work folder that is visible to the MATLAB® search path. This example uses
C:\Work
as the new work folder.Install MATLAB Runtime on Windows and on additional platforms where you plan on running your .NET 5.0 application. For details, see Install and Configure MATLAB Runtime.
For Linux and macOS platforms, after installing MATLAB Runtime, you need to set the
LD_LIBRARY_PATH
andDYLD_LIBRARY_PATH
environment variables respectively. For more information, see Set MATLAB Runtime Path for Deployment.Verify that you have Visual Studio® and .NET 5.0 or higher installed. If you have version 16.8.0 of Visual Studio 2019 installed, then you do not need to install .NET 5.0 or higher separately.
Create .NET Assembly
Package a function into a .NET assembly using the Library Compiler app.
Alternatively, if you want to create a .NET assembly from the MATLAB command window using a programmatic approach, see compiler.build.dotNETAssembly
.
Create a new MATLAB file named
mymagic.m
with the following code in the work folder:function out = mymagic(in) out = magic(in);
Type
libraryCompiler
at the MATLAB command line to launch the Library Compiler app.In the TYPE section of the toolstrip, select
.NET Assembly
, and in the EXPORTED FUNCTIONS section, click the Add button to add the filemymagic.m
to the project.In the Library information section, name the library
MyMatrixFunctions
.Double-click the class
Class1
and rename it toMyMagic
.Expand the Additional runtime settings section and select
5.0
as the .NET version.Note
.NET 5.0 does not support type-safe API.
Save the deployment project with the project name
MyMatrixFunctions
.Select Package to create a .NET assembly. For information about the created files, see Files Generated After Packaging MATLAB Functions.
Create .NET 5.0 Application
If you are using Visual Studio, see Create .NET 5.0 App Using Visual Studio.
Open the command prompt in Windows and navigate to the folder
C:\Work
.At the command line, type:
dotnet new console --framework net5.0 --name MyDotNet5App
This creates a folder named
MyDotNet5App
that has the following contents:obj
folderMyDotNet5App.csproj
project fileProgram.cs
C# source file
Open the project file in a text editor.
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0</TargetFramework> </PropertyGroup> </Project>
Add the following references to the project using the
<ItemGroup>
tag:.NET assembly file
MyMatrixFunctions.dll
created by the Library Compiler appMWArray.dll
, which is located in<MATLAB_RUNTIME_INSTALL_DIR>
\toolbox\dotnetbuilder\bin\win64\<framework_version>
Once you add the references, your project file should resemble the following:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0</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\R2022b\toolbox\dotnetbuilder\bin\win64\v5.0\MWArray.dll</HintPath> <!--Path to MWArray.dll in the MATLAB Runtime--> </Reference> </ItemGroup> </Project>
Open the C# source file
Program.cs
and replace the existing code with the following code:At the command line, build your .NET 5.0 project by typing:
dotnet build MyDotNet5App.csproj
At the command line, run your application by typing:
dotnet run -- 3
The application displays a 3-by-3 magic square.
Publish the project as a self-contained deployment to execute the application on either Linux or macOS.
To publish to Linux, type the following command on a single line:
dotnet publish --configuration Release --framework net5.0 --runtime linux-x64 --self-contained true MyDotNet5App.csproj
To publish to macOS, type the following command on a single line:
dotnet publish --configuration Release --framework net5.0 --runtime osx.10.11-x64 --self-contained true MyDotNet5App.csproj
Create .NET 5.0 App Using Visual Studio
As an alternative to the interactive command line approach to creating a .NET 5.0 application, you can create a .NET 5.0 application using Microsoft® Visual Studio. If you have already created a .NET 5.0 application using the above instructions, you can skip this section.
In Visual Studio create a .NET 5.0 C# project named
MyDotNet5App
. For details, see Create the app section in Create a .NET console application using Visual Studio.In the Solution Explorer in Visual Studio, right-click the project name and select Add > Project Reference. In the Reference Manager window, click Browse and add the following references:
C:\work\MyMatrixFunctions\for_redistribution_files_only\MyMatrixFunctions.dll
C:\Program Files\MATLAB\MATLAB Runtime\R2022b\toolbox\dotnetbuilder\bin\win64\v5.0\MWArray.dll
Open the C# source file
Program.cs
and replace the existing code with the code provided in step 4 in the above section.Build and run the application.
Run .NET Core Application on Linux
Copy the
Release
folder fromC:\Work\MyDotNet5App\bin
on Windows to~/Work
on a Linux or macOS machine.On the Linux machine, verify that you have installed MATLAB Runtime and set up your library path environment variable. For more information, see Prerequisites.
Open a command shell and navigate to:
~/Work/Release/net5.0/<os-architecture>/publish
Run the .NET 5.0 application by typing:
./MyDotNet5App 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