A god-tier project template for developing cross-platform C# Garry's Mod (Lua) binary modules, built with .NET 11 and CMake, featuring .NET NativeAOT (ahead-of-time compilation) and direct native interop with static/shared native C library from C#... This is still WIP, but you are welcome to use it...
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 |
|---|---|---|
IsClientModule |
true |
Whether this is a client-side module (defines CLIENT constant, produces gmcl_* binaries; otherwise, produces gmsv_* binaries) |
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 |
|---|---|---|
CLIENT |
If IsClientModule is set to true | #if CLIENT for client-side code |
SERVER |
When IsClientModule is false | #if SERVER for server-side code |
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 void M()
{
#if CLIENT
#if NATIVE_AOT
// NativeAOT client-side code
#else
// Managed client-side code
#endif
#else
// Server-side code (when IsClientModule=false)
#endif
#if WINDOWS
// Windows-specific code
#elif LINUX
// Linux-specific code
#elif MACOS
// macOS-specific code
#endif
}Builds the C# binary 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 binary 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)
- Garry's Mod (and a pair of hands)
Linux / macOS:
- GCC (preferred) or Clang
- .NET 11 SDK
- PowerShell 7+ (pwsh)
- Garry's Mod (and a pair of hands)
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# binary 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 above├── dnGLua.BinaryModule/
│ ├── dnGLua.BinaryModule.csproj # C# MSBuild project
│ ├── Main.cs # Main C# code, binary module entry-point
│ └── 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 binary module output directory
├── bin-managed/ # Managed binary 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/GMod (x86) - this is a .NET limitation (use '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 assembly with proper Garry's Mod binary module naming
Note
By default IsClientModule is true, so the build produces the client-side gmcl_* binary module.
Set it to false in the csproj to build the server-side gmsv_* variant instead.
Note: Garry's Mod Lua binary modules always use the .dll file extension regardless of platform. On
Linux and macOS the file is actually an ELF shared object (.so) or Mach-O dynamic library (.dylib)
with a .dll name; the build scripts handle the renaming automatically.
| Platform | Client Module | Server Module |
|---|---|---|
| Windows 64-bit | gmcl_mymodule_win64.dll |
gmsv_mymodule_win64.dll |
| Windows 32-bit | gmcl_mymodule_win32.dll |
gmsv_mymodule_win32.dll |
| Linux 64-bit | gmcl_mymodule_linux64.dll |
gmsv_mymodule_linux64.dll |
| macOS 64-bit | gmcl_mymodule_osx64.dll |
gmsv_mymodule_osx64.dll |
- Edit
mynativelib/mynativelib.c - Rebuild native library with
build-native.ps1 - Rebuild binary module with
build-module.ps1
Note
With the default CompileMyNativeLibAsDll=true, the module links against the import library and
mynativelib.dll must be copied next to gmcl_*.dll in garrysmod/lua/bin/ 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 binary module with
build-module.ps1
- Ensure you build the binary module using the same target architecture as your Garry's Mod process/branch (x64 vs x86/Win32)
- Ensure you install it correctly by copying all relevant files into
.../garrysmod/lua/binfolder (create thebinfolder if you don't have it), it's helpful to use a symlink for a smoother "auto-update on build" experience (Google it) - Ensure you don't rename the binary module file, it has to follow the Garry's Mod binary module naming convention
- Ensure you don't modify/break the csproj/msbuild code by accident
- Ensure you have the correct permissions to access the
.../garrysmod/lua/binfolder
Warning
If Garry's Mod crashes with exit code 0xC0000409 when running require("mymodule") and a managed
debugger reports that the process exited without a CoreCLR started event, you copied the managed
(non-AOT) build. Only the NativeAOT binary is self-contained: use the gmcl_*.dll 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 mymodule.runtimeconfig.json next to it, and crashes the game when
they are missing.
- 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
