Using MeshLib with C#
This instruction will guide you through integrating MeshLib into your C# project using NuGet or the .NET Command-Line Interface.
- Note
- MeshLib C# bindings are fully generated from the C++ codebase and expose the complete public API, with the exception of the MR.Viewer module. The C# API is fully compatible with Unity — see Using MeshLib with Unity.
-
The C# bindings are production-ready. The full, stable C# API reference and comprehensive code samples are available and maintained. C# support is provided consistently across all supported platforms, ensuring feature parity with the native C++ API.
Prerequisites
Before installing MeshLib, ensure you have the following tools installed:
Supported platforms
The NuGet package ships prebuilt native libraries, so your OS and CPU architecture must be one it covers:
| OS | Architectures | Minimum version |
| Windows | x64, Arm64 | Windows 10 |
| Linux | x64, Arm64 | glibc 2.28+ — e.g. Ubuntu 20.04+, Debian 10+, RHEL/Rocky/AlmaLinux 8+, Fedora 29+ |
| macOS | Arm64 (Apple silicon) | macOS 14 |
| macOS | x64 (Intel) | macOS 15 |
- Note
- On Windows on Arm, .NET 5 and newer resolve win-arm64 by themselves. A .NET Framework project instead gets the runtime named by its PlatformTarget, so set <PlatformTarget>ARM64</PlatformTarget> to run natively; any other value gives it the win-x64 libraries and the x64 emulation. Native Arm64 .NET Framework requires 4.8.1, i.e. Windows 11.
On Linux no extra system packages are required: beyond the C and C++ runtimes the libraries need only libexpat1 and zlib1g, which a standard desktop or server install already provides. Every other dependency is bundled in the package.
On macOS the floor differs by architecture: part of the file-format libraries in the x64 runtime are built against a newer SDK, so on an Intel Mac running macOS 14 the core API loads but the extra formats (E57, LAZ, CTM) fail.
.NET SDK
- Install the latest version of the .NET SDK from the .NET website. MeshLib targets netstandard2.0, so it sets no upper bound on your .NET version: it is compatible with .NET Framework 4.7 and higher, and with .NET 5.0 and every later version.
- Verify your installation by running:
Visual Studio
If you're using Visual Studio, make sure to install the .NET desktop development workload.
Installation via NuGet
Depending on your development environment and workflow preferences, you can choose from the following installation methods.
Using Visual Studio on Windows
To install MeshLib via Visual Studio, follow these steps:
- Open Your Project in Visual Studio:
- If you don’t have a project yet, create a new C# Console App (.NET Core) or another suitable project type.
- Open the NuGet Package Manager:
- Right-click on your project in the Solution Explorer.
- Select "Manage NuGet Packages."
- Search for MeshLib:
- In the NuGet Package Manager, switch to the "Browse" tab.
- Enter "MeshLib" in the search box.
- Install the Package:
- Select the MeshLib package from the search results.
- Click "Install" and follow any prompts to complete the installation.
- Start Using MeshLib:
- You should now have access to the static class MR and its members.
- Confirm the native libraries actually load: Verify Your Installation.
Using JetBrains Rider
Rider runs on Windows, macOS, and Linux; the steps are the same on all three:
- Open Your Project in Rider:
- If you don’t have a project yet, create a new solution from the Console Application template.
- Open the NuGet Tool Window:
- Select Tools → NuGet → Manage NuGet Packages for Solution.
- Search for MeshLib:
- On the Packages tab, enter "MeshLib" in the search box.
- Install the Package:
- Select the MeshLib package, pick your project on the right, and click + (Install).
- Start Using MeshLib:
- You should now have access to the static class MR and its members.
Using Visual Studio Code with the C# Dev Kit
VS Code runs on Windows, macOS, and Linux; the steps are the same on all three:
- Install the C# Dev Kit:
- In the Extensions view, search for "C# Dev Kit" and install it. It brings the Solution Explorer used below.
- Open Your Project in VS Code:
- Open the folder containing your .csproj or .sln. If you don’t have a project yet, create one with dotnet new console.
- Add the Package:
- In the Solution Explorer view, right-click your project and select Add NuGet Package.
- Alternatively, run the .NET: Add NuGet Package command from the Command Palette (Ctrl+Shift+P, on macOS Cmd+Shift+P).
- Search for MeshLib:
- Enter "MeshLib" and select the package, then confirm the version to install.
- Start Using MeshLib:
- You should now have access to the static class MR and its members.
- Note
- Using another editor, or no editor at all? The .NET Command-Line Interface works the same way on Windows, macOS, and Linux, and needs nothing but the .NET SDK.
Installation from a Downloaded NuGet Package
If you prefer to download the MeshLib package directly from the NuGet website, follow these steps:
- Download the MeshLib Package:
- Create a Local Directory for NuGet Packages:
- Choose or create a directory on your system where you will store the .nupkg file. For example:
- Windows (Command Prompt):
mkdir %USERPROFILE%\LocalNuGetPackages
- Linux / macOS:
mkdir ~/LocalNuGetPackages
- Move the downloaded package to that directory keeping its meshlib.<version>.nupkg file name.
- Install the MeshLib Package Locally:
- Use the command below to install the package:
- Windows (Command Prompt):
dotnet add package MeshLib --source %USERPROFILE%\LocalNuGetPackages
- Linux / macOS:
dotnet add package MeshLib --source ~/LocalNuGetPackages
- Alternative: Configure Local Source Globally:
- Add the local directory as a source using:
- Windows (Command Prompt):
dotnet nuget add source %USERPROFILE%\LocalNuGetPackages --name LocalPackages
- Linux / macOS:
dotnet nuget add source ~/LocalNuGetPackages --name LocalPackages
- After this, you can install the package normally without specifying --source.
- Build and Run Your Project:
- An untouched project prints only its template output, which says nothing about MeshLib. Continue with Verify Your Installation.
Installation via .NET Command-Line Interface
To install MeshLib via the .NET CLI, follow these steps:
- Create a New Project Directory:
Run the following commands to create a new directory for your project and navigate into it: mkdir TestProject
cd TestProject
- Create a New .NET Console Project:
Inside the new directory, initialize a .NET console project by running:
- Install the MeshLib Package:
Use the following command to add MeshLib to your project: dotnet add package MeshLib
- Build and Run Your Project:
After installing MeshLib, you can build and run your project using:
- Verify MeshLib Works:
dotnet new console writes no MeshLib code, so the output above is the same whether MeshLib loaded or not. Continue with Verify Your Installation.
By following these steps, you can integrate MeshLib into your project, giving you more control over the setup process.
Verify Your Installation
Adding the package and building the project succeed even when the native libraries cannot be loaded, so the setup is only proven by a real MeshLib call. Replace the contents of Program.cs with:
var mesh = MR.makeCube();
Console.WriteLine($"MeshLib OK: cube has {mesh.points.size()} points, "
+ $"{mesh.topology.numValidFaces()} faces");
MR.MeshSave.toAnySupportedFormat(mesh, "cube.stl");
Console.WriteLine("Wrote cube.stl");
Then run:
The expected output is exactly:
MeshLib OK: cube has 8 points, 12 faces
Wrote cube.stl
and a cube.stl of about 700 bytes appears in the working directory.
Package Size and Native Runtimes
The NuGet package carries prebuilt native libraries for all six supported platforms, so it is large: 200+ MB Nuget download, 700+ MB copied to the build directory by default.
For deployment, you might wish to publish for a single RID (single platform), which omits the unnecessary libraries for other platforms, e.g.:
dotnet publish -r linux-x64 --self-contained false
This copies only about 180 MB of libraries to the output directory, as opposed to 700+ MB.
Supported platform IDs are: win-x64, win-arm64, linux-x64, linux-arm64, osx-x64, osx-arm64.
Using MeshLib with Unity
Unity projects consume the same NuGet package via NuGetForUnity; this setup is continuously tested with Unity 6 on Windows, Linux, and macOS. To install MeshLib into a Unity project:
- Install NuGetForUnity:
- In the Unity editor, open Window → Package Manager, click +, select "Install package from git URL...", and enter:
https://github.com/GlitchEnzo/NuGetForUnity.git?path=/src/NuGetForUnity
- Install MeshLib:
- Open NuGet → Manage NuGet Packages, switch to the "Online" tab, search for "MeshLib", and click "Install".
Troubleshooting
The C# assembly is a thin managed wrapper over MeshLib's native libraries, so setup problems typically surface not when the package is installed or the project is built, but as one of the following exceptions on the first call into MeshLib.
DllNotFoundException
System.DllNotFoundException: Unable to load DLL 'MeshLibC2' or one of its dependencies: The specified module could not be found.
(on Linux and macOS: Unable to load shared library 'MeshLibC2' or one of its dependencies)
The native libraries were not found at run time:
- Check that your platform is supported.
- Check that the native libraries reached your build output: on .NET they are placed under runtimes/<rid>/native/ in the output directory (see Package Size and Native Runtimes), on .NET Framework next to the executable (MeshLibC2.dll and its dependencies on Windows).
BadImageFormatException
System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
MeshLib's native libraries are 64-bit only, and this exception means your application runs as a 32-bit process. .NET Framework projects are prone to this: with the default AnyCPU platform and Prefer 32-bit checked, the application runs 32-bit even on 64-bit Windows. In the project properties (Build tab) set Platform target to x64 or uncheck Prefer 32-bit — in the .csproj terms:
<PlatformTarget>x64</PlatformTarget>
<Prefer32Bit>false</Prefer32Bit>
On Windows on Arm use ARM64 rather than x64 there: for .NET Framework projects that value also selects which of the package's native runtimes gets copied.
A rebuilt local package is ignored
When installing from a local directory, NuGet extracts the package into its global cache and keeps serving the cached copy for the same version number, ignoring a rebuilt .nupkg. Delete the cached copy and restore again:
rm -rf ~/.nuget/packages/meshlib
On Windows, delete USERPROFILE%\.nuget\packages\meshlib.
Try MeshLib with C# Examples
MeshLib provides a collection of C# code samples to help you get started quickly with common mesh processing tasks. These are concise, self-contained examples — perfect for exploring how to use MeshLib’s API in your own projects:
- Load and Save Meshes Demonstrates how to load and export mesh files using the MeshLib API.
- Mesh Boolean Operations Shows how to perform boolean operations like union, intersection, and subtraction between meshes.
- Mesh Simplification Illustrates how to reduce mesh complexity through decimation, preserving overall shape with fewer polygons.
These samples are a great starting point for integrating MeshLib into C# or .NET-based workflow.