Developing on Linux but need to test a Windows .dll? Zig ships a single toolchain that can cross-compile for any target, and Affix::Build speaks Zig natively via lang => 'zig'. By passing a -target flag, you can produce Windows binaries straight from a Linux box.
The Recipe
We will configure Affix::Build to target Windows x86_64 from a Linux machine using Zig's built-in cross-compilation.
use v5.40;
use Affix::Build;
# 1. Define the cross-compilation target
# This requires the 'zig' binary to be in your PATH
my $target = "x86_64-windows-gnu";
my $c = Affix::Build->new( name => 'cross_lib' );
# Zig source is C-ABI compatible: use the 'export' keyword.
# The '-target' flag is forwarded to 'zig build-lib'.
$c->add( \<<~'ZIG', lang => 'zig', flags => [ '-target', $target ] );
export fn add(a: i32, b: i32) i32 {
return a + b;
}
ZIG
# 2. Link produces a Windows DLL even on Linux!
my $dll_path = $c->link;
say "Windows library created at: $dll_path";
How It Works
-
Toolchain Abstraction
Affix::Buildresolveslang => 'zig'to thezig build-libcommand and forwards any flags you pass viaadd'sflagsparameter. Because Zig ships the headers, stdlibs, and linkers for Windows, macOS, and Linux inside a single binary, cross-compiling is just a matter of swapping the-targetvalue. -
The
.dllresult The-target x86_64-windows-gnutells Zig to emit a Portable Executable (PE) file. The output is written toAffix::Build's default name, which follows your host platform's shared-library convention (e.g.,libcross_lib.soon Linux) — but the contents are a Windows DLL. Copy the file to a Windows machine and load it withAffix::load_library.
Comments