A project template for developing cross-platform C# Lua binary modules, built with .NET 11 and CMake, featuring .NET NativeAOT (ahead-of-time compilation) and direct native interop with a static/shared native C library from C#.
The produced module is a standard Lua C module: require("mymodule") resolves to luaopen_mymodule
in mymodule.dll (Windows), mymodule.so (Linux), or mymodule.dylib (macOS).
This project uses a modern cross-platform build system:
- Native Library: Built with CMake
- Managed Code: Built with .NET SDK (
dotnet) - Build Scripts: PowerShell (
pwsh)
The project supports several configurable MSBuild properties that can be set in the project file (.csproj) or on the command line:
| Property | Default | Description |
|---|---|---|
AssemblyName |
mymodule |
Module name: the Lua file name and luaopen_<name> entry point must match it |
DisableOptimization |
false |
Whether to disable code optimizations |
DisablePdb |
false |
Whether to disable PDB generation |
BuildNativeLibrary |
false |
Whether to build the native C library (set to true to build) |
CompileMyNativeLibAsDll |
true |
Compile the native library as a shared DLL (true) or static library (false). On Windows the static archive is named mynativelib_static.lib so it does not collide with the shared import library mynativelib.lib |
PublishAot |
false |
Enable NativeAOT publishing (normally passed via /p:PublishAot=true or the build scripts) |
<PropertyGroup>
<!-- Build the 'mynativelib' library -->
<BuildNativeLibrary>true</BuildNativeLibrary>
<!-- Compile it as static library instead of DLL (requires BuildNativeLibrary and PublishAot) -->
<CompileMyNativeLibAsDll>false</CompileMyNativeLibAsDll>
</PropertyGroup>The project defines several constants that can be used for conditional compilation in C# code:
| Constant | When Defined | Usage |
|---|---|---|
NATIVE_AOT |
When building with -Type aot |
#if NATIVE_AOT to distinguish AOT vs managed builds |
X64 |
When building for x64 platform | #if X64 for platform-specific code |
X86 |
When building for x86 platform | #if X86 for platform-specific code |
WINDOWS |
When building Windows version | #if WINDOWS for OS-specific code |
LINUX |
When building Linux version | #if LINUX for OS-specific code |
MACOS |
When building macOS version | #if MACOS for OS-specific code |
The build also defines RID-derived constants: ARCH_X64/ARCH_X86/ARCH_ARM64/ARCH_ARM with matching
TARGET_* names, PLATFORM_WINDOWS/PLATFORM_LINUX/PLATFORM_OSX, and _WIN64/_WIN32.
public static int LuaOpen(void* L)
{
#if NATIVE_AOT
// NativeAOT-specific code
#else
// Managed-specific code
#endif
#if WINDOWS
// Windows-specific code
#elif LINUX
// Linux-specific code
#elif MACOS
// macOS-specific code
#endif
return 0;
}Builds the C# Lua module with support for AOT and managed compilation.
| Parameter | Short Form | Description | Values |
|---|---|---|---|
-Type |
-t |
Build type (case-insensitive) | aot, managed, both (default) |
-Clean |
-c |
Clean build artifacts before building | Switch parameter |
-Help |
Show help information | Switch parameter |
- Case-insensitive build types: Use
AOT,aot,Managed,managed, etc. - Short form parameters: Use
-tfor-Typeand-cfor-Clean - Smart clean behavior:
-Cleanalone only cleans,-Cleanwith-Typecleans then builds - Selective building: Build only AOT or only managed versions as needed
- Automatic cleanup: Cleans all build artifacts including
bin/,bin-aot/,bin-managed/, andobj/directories - Automatic MSVC toolset detection (Windows): The AOT build locates Visual Studio via
vswhere, picks a working vcvars toolset (including preview-only installs), and publishes inside the vcvars environment, so no manual Developer Command Prompt is needed - Cross-platform: Works on Windows, Linux, and macOS through PowerShell 7. On Linux the AOT build
uses the system
clangtoolchain and requireszlib1g-devandlibicu(e.g.sudo apt install clang zlib1g-dev libicu-dev)
# Build both AOT and managed modules
./build-module.ps1
# Build specific module type (case-insensitive)
./build-module.ps1 -t aot
./build-module.ps1 -t managed
./build-module.ps1 -t AOT # Case-insensitive
./build-module.ps1 -Type Managed # Case-insensitive
# Clean only (no build)
./build-module.ps1 -c
./build-module.ps1 -Clean
# Clean then build specific type
./build-module.ps1 -t aot -c
./build-module.ps1 -t managed -cBuilds the native C library using CMake with cross-platform support.
Important
Required before building the Lua module: Main.cs calls into mynativelib via
DllImport/NativeImport, and BuildNativeLibrary defaults to false, so build-module.ps1 does
not build it for you.
On Windows, artifacts/runtimes/<rid>/native/ ends up containing:
| File | Produced by | Linked when |
|---|---|---|
mynativelib.dll |
shared build | CompileMyNativeLibAsDll=true (runtime dependency) |
mynativelib.lib |
shared build | CompileMyNativeLibAsDll=true (import library for the DLL) |
mynativelib_static.lib |
static build | CompileMyNativeLibAsDll=false (static archive, self-contained) |
The _static suffix exists because MSVC names both the static archive and the DLL import library
mynativelib.lib; without it the two builds would overwrite each other in the same directory. The
csproj selects the correct file automatically based on CompileMyNativeLibAsDll.
| Parameter | Short Form | Description | Values |
|---|---|---|---|
-Platforms |
-p, -Platform |
Build specific platforms only (case-insensitive) | See supported platforms below |
-Clean |
Clean up all build files and artifacts | Switch parameter | |
-StaticOnly |
Build only static libraries | Switch parameter | |
-SharedOnly |
Build only shared libraries | Switch parameter | |
-Help |
Show help information | Switch parameter |
| Platform | Short Form | Description |
|---|---|---|
windows-x64 |
win-x64 |
Windows 64-bit |
windows-x86 |
win-x86 |
Windows 32-bit |
linux-x64 |
Linux 64-bit | |
osx-x64 |
macOS 64-bit (Intel) |
- Cross-platform support: Works on Windows, Linux, and macOS
- Auto-detection: Automatically detects current platform and available architectures
- Multiple platform builds: Can build for multiple platforms in one command
- Library type control: Build static only, shared only, or both
- Compiler detection: Automatically detects and prefers GCC over Clang on Linux/macOS
- Hidden build directories: Uses
.build-*directories to keep project clean - Case-insensitive platforms: Use
WINDOWS-X64,linux-x64, etc.
The script automatically detects platforms:
- Windows x64: Builds both
windows-x64andwindows-x86 - Windows x86: Builds only
windows-x86 - Linux: Builds for current architecture (x64)
- macOS: Builds for current architecture (x64)
# Build detected platforms (by default builds static + shared)
./build-native.ps1
# Build specific platforms (case-insensitive)
./build-native.ps1 -Platforms windows-x64,linux-x64,osx-x64
./build-native.ps1 -Platform WINDOWS-X64,LINUX-X64,OSX-X64
./build-native.ps1 -p windows-x64,linux-x64,osx-x64
./build-native.ps1 -p WIN-X64,win-x86 # Short platform names, mixed case
# Build only static or shared libraries
./build-native.ps1 -StaticOnly
./build-native.ps1 -SharedOnly
# Build specific platforms with library type control (case-insensitive)
./build-native.ps1 -p LINUX-X64 -StaticOnly
./build-native.ps1 -p windows-x64,win-x86 -SharedOnly
# Clean up all build files and artifacts
./build-native.ps1 -CleanWindows:
- Visual Studio 2022/2026 with Windows 10/11 SDK and build tools (C++ workload)
- .NET 11 SDK
- PowerShell 7+ (pwsh)
- A Lua interpreter or host application (PUC-Rio Lua, LuaJIT, etc.)
Linux / macOS:
- GCC (preferred) or Clang
- .NET 11 SDK
- PowerShell 7+ (pwsh)
- A Lua interpreter or host application (PUC-Rio Lua, LuaJIT, etc.)
Important
The C# project requires preview language features (LangVersion=preview with the C# 15
updated-memory-safety-rules feature, provided by the .NET 11 SDK) and NativeInvoke 1.6.1+.
# Build native library first (required: Main.cs P/Invokes into mynativelib)
./build-native.ps1
# Then build C# Lua module (both AOT and managed)
# On Windows the AOT build auto-detects the MSVC toolset via vswhere and
# initializes vcvars64 itself, so it also works on VS Insiders installs
./build-module.ps1
# For more options and examples, see the Build Script Features section aboveCopy the built module into your Lua environment's module search path and require it:
-- Windows: mymodule.dll, Linux: mymodule.so, macOS: mymodule.dylib
package.cpath = package.cpath .. ";./?.dll;./?.so;./?.dylib"
require("mymodule")├── Lua.BinaryModule/
│ ├── Lua.BinaryModule.csproj # C# MSBuild project
│ ├── Main.cs # Module entry point (luaopen_mymodule)
│ └── GlobalUsings.cs # Global using statements
├── mynativelib/ # C native library
│ └── mynativelib.c # Example native C source code
├── .build-*/ # Intermediate build directory for native library
├── CMakeLists.txt # CMake configuration for native C library
├── build-native.ps1 # Cross-platform native library build script
├── build-module.ps1 # Unified AOT and managed module build script
├── bin-aot/ # AOT module output directory
├── bin-managed/ # Managed module output directory
└── artifacts/ # Native reference directory
└── runtimes/
├── win-x64/native/
├── win-x86/native/
├── linux-x64/native/
└── osx-x64/native/
The CMake build system automatically:
- Detects the platform and architecture
- Auto-selects compiler (GCC preferred, then Clang on Linux/macOS)
- Configures appropriate compiler settings
- Outputs libraries to the correct
artifacts/runtimes/directory - Supports both static and shared library builds
- On Windows x64: Builds both x86 and x64 versions
- Creates hidden build directories in project root (
.build-*) to keepmynativelib/clean - CMakeLists.txt is located in project root for easier access
- Windows: x64, x86* (MSVC, Visual Studio 2022/2026)
- Linux / macOS: x64 (GCC preferred, then Clang)
Note: NativeAOT compilation is not supported for 32-bit Windows (x86) - this is a .NET limitation (use the 'managed' version instead).
The simplified MSBuild project:
- Uses modern .NET SDK style project
- Supports cross-platform building
- Automatically links with pre-built native library
- Generates the module file with the module name (
mymodule.dll/.so/.dylib)
| Platform | AOT Build | Managed Build |
|---|---|---|
| Windows 64-bit | mymodule.dll |
mymodule.dll + mymodule.managed.dll + runtimeconfig |
| Windows 32-bit | mymodule.dll |
mymodule.dll + mymodule.managed.dll + runtimeconfig |
| Linux 64-bit | mymodule.so |
mymodule.so + mymodule.managed.so + runtimeconfig |
| macOS 64-bit | mymodule.dylib |
mymodule.dylib + mymodule.managed.dylib + runtimeconfig |
Note
The managed build produces two files: the native shim Lua loads (mymodule.dll/.so/.dylib,
which hosts the CLR and exports luaopen_mymodule) and the managed assembly
(mymodule.managed.*, suffixed so it does not collide with the shim). The managed variant
additionally needs mymodule.managed.runtimeconfig.json and mymodule.managed.deps.json
next to it; only the NativeAOT build is fully self-contained.
- Edit
mynativelib/mynativelib.c - Rebuild native library with
build-native.ps1 - Rebuild Lua module with
build-module.ps1
Note
With the default CompileMyNativeLibAsDll=true, the module links against the import library and
mynativelib.dll/libmynativelib.so/libmynativelib.dylib must sit next to the module file at
runtime. Set CompileMyNativeLibAsDll=false (and rebuild both the native static lib and the module)
to link the code statically instead, which keeps the binary self-contained.
- (TODO: write a guide)
- Rebuild Lua module with
build-module.ps1
- Ensure you build the module using the same target architecture as your Lua interpreter/host (x64 vs x86/Win32)
- Ensure the module file lands in a directory on Lua's
package.cpathand keeps the exact module name (require("mymodule")needsmymodule.dll/.so/.dylib) - Ensure you don't rename the module file or the
luaopen_mymoduleentry point - Ensure you don't modify/break the csproj/msbuild code by accident
Warning
If the Lua host crashes when running require("mymodule") and a debugger reports that the process
exited without a CoreCLR started event, you copied the managed (non-AOT) build without its support
files. Only the NativeAOT binary is self-contained: use the output produced by
dotnet publish /p:PublishAot=true (the bin-aot/ output). The managed build is a small CoreCLR
shim that needs the .NET runtime and mynodule.managed.runtimeconfig.json next to it.
- Windows: Ensure Visual Studio 2022/2026 with Windows 10/11 SDK and C++ build tools are installed
- Linux: Install
build-essentialor equivalent (GCC preferred) - macOS: Install Xcode Command Line Tools:
xcode-select --install(GCC preferred via Homebrew) - Build directories: Build artifacts are stored in hidden
.build-*directories in the project root, not inmynativelib/ - Platform-specific issues: Use
./build-native.ps1 -Helpto see supported platforms and options - Compiler detection: Script automatically prefers GCC over Clang on Linux/macOS; install GCC if build fails
- Multiple platforms: When building for multiple platforms, ensure all required toolchains are installed
Tip
Visual Studio Insiders / preview-only MSVC: On a machine where only preview MSVC toolsets are
installed, a plain dotnet publish /p:PublishAot=true fails with "vswhere.exe failed to locate
Visual Studio" (ILCompiler) or the DNNE findvcvarsall.bat step fails, because vswhere cannot find
the Microsoft.VisualStudio.Component.VC.Tools.x86.x64 component. build-module.ps1 -t aot handles
this automatically: it locates Visual Studio via vswhere, initializes vcvars64.bat inside the
publish (pinning -vcvars_ver to the newest toolset when the stable default named by
v145.default.txt has no matching props file, which is the preview-only case where the default
toolset's lib directory can miss LIBCMT.lib), passes /p:IlcUseEnvironmentalTools=true to bypass
vswhere entirely, and runs dotnet build-server shutdown first because stale MSBuild nodes keep the
environment they were started with.
- Ensure .NET 11 SDK is installed
- Run
dotnet restore --forcebefore building - Optionally, check that the native library exists in
artifacts/runtimes/ - Use
./build-module.ps1 -Helpto see all available options - If build produces both
bin-aotandbin-managedwhen you only want one, ensure you're specifying the correct-Typeparameter - Use
./build-module.ps1 -cto clean all build artifacts including output directories