Ubuntu(Mono环境)下C#程序如何引用MATLAB Runtime库?
Hey there, let's tackle your problem head-on—this is totally feasible, but the key issue here is that you're trying to use the Windows-specific MWArray.dll on Linux, which won't work. Linux uses shared object files (.so) under the hood, and MATLAB Runtime provides a Linux-compatible .NET binding for this. Here's how to fix it step by step:
1. Confirm Version Compatibility First
Before anything else, make sure the MATLAB version you used to build your C# program matches exactly with the MATLAB Runtime installed on your Ubuntu machine. Even minor version mismatches (like R2023a vs R2023b) can cause library not found errors.
You can check your MATLAB Runtime version by navigating to its default install path:
ls /usr/local/MATLAB/MATLAB_Runtime/
This will show you folders named like R20XXx (e.g., R2023a)—that's your Runtime version.
2. Replace the Windows MWArray.dll with Linux's Compatible Version
Windows' MWArray.dll isn't built for Linux. Instead, grab the Linux-compatible MWArray.dll directly from your installed MATLAB Runtime. It's located in the extern/dotnet directory of your Runtime installation. For example:
/usr/local/MATLAB/MATLAB_Runtime/R20XXx/extern/dotnet/netstandard2.0/MWArray.dll
Copy this file to your C# project's reference folder, replacing the Windows version you were using before.
3. Configure Library Paths for Mono
Mono needs to find MATLAB Runtime's underlying shared libraries (.so files) to run your program. Set the LD_LIBRARY_PATH environment variable to include the necessary Runtime directories:
Temporary Setup (for current terminal session)
Run this command before launching your program:
export LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Runtime/R20XXx/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/R20XXx/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/R20XXx/sys/os/glnxa64:$LD_LIBRARY_PATH
Permanent Setup (for all terminal sessions)
Add the above line to your ~/.bashrc or ~/.profile file, then run:
source ~/.bashrc
This ensures the path is set every time you open a terminal.
4. Compile & Run Your C# Program with Mono
Compile
Use Mono's C# compiler (mcs) and reference the Linux version of MWArray.dll:
mcs YourProgram.cs -r:/path/to/your/copied/MWArray.dll
Replace YourProgram.cs with your actual C# file name, and update the path to the copied MWArray.dll.
Run
Launch the compiled executable with Mono:
mono YourProgram.exe
5. Troubleshooting Common Issues
- Still getting "library not found" errors? Double-check that
LD_LIBRARY_PATHincludes all three directories mentioned earlier, and that your Runtime version matches your program's MATLAB version. - Mono compatibility issues? Ensure your Mono version supports the .NET Framework version your C# program was built with. For example, if you used .NET Framework 4.8, you'll need Mono 6.12 or newer.
- Permission issues? Make sure you have read access to the MATLAB Runtime directories and the copied
MWArray.dll.
内容的提问来源于stack exchange,提问作者Roshan Vincent

