Skip to content

Instantly share code, notes, and snippets.

@rdunnington
Last active September 9, 2025 04:27
Show Gist options
  • Select an option

  • Save rdunnington/50db7b41c3876294c3159fa9a2ebad77 to your computer and use it in GitHub Desktop.

Select an option

Save rdunnington/50db7b41c3876294c3159fa9a2ebad77 to your computer and use it in GitHub Desktop.
Zig dynamic linking
const std = @import("std");
const Build = std.Build;
pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const lib = b.addLibrary(.{
.name = "foo",
.linkage = .dynamic,
.root_module = b.createModule(.{
.optimize = optimize,
.target = target,
.link_libc = true,
}),
});
lib.addCSourceFiles(.{
.flags = &.{},
.files = &.{"foo.c"},
});
const install_lib = b.addInstallArtifact(lib, .{});
const exe = b.addExecutable(.{
.name = "main",
.root_module = b.createModule(.{
.optimize = optimize,
.target = target,
.link_libc = true,
}),
});
lib.addCSourceFiles(.{
.flags = &.{},
.files = &.{"main.c"},
});
exe.linkLibrary(lib);
const install_exe = b.addInstallArtifact(exe, .{});
b.getInstallStep().dependOn(&install_lib.step);
b.getInstallStep().dependOn(&install_exe.step);
}
clang -g -shared foo.c -o foo.dll
clang -g main.c -lfoo -o main.exe
E:\Dev\scratch\clang-zig-link> zig build
install
└─ install foo
└─ zig build-lib foo Debug native failure
error: warning(link): unexpected LLD stderr:
lld-link: warning: E:\Dev\scratch\clang-zig-link\.zig-cache\o\c97d6dfd09e9d74e36939460aeeb701b\main.obj: locally defined symbol imported: foo (defined in E:\Dev\scratch\clang-zig-link\.zig-cache\o\cb7e0039b5b349849cda2da3aaf42ea0\foo.obj) [LNK4217]
install
└─ install main
└─ zig build-exe main Debug native 4 errors
error: lld-link: undefined symbol: RegisterDialogClasses
note: referenced by E:\tools\zig\zig-x86_64-windows-0.14.1\lib\libc\mingw\libsrc\scrnsave.c:0
note: libmingw32.lib(scrnsave.obj):(WinMain)
note: referenced by E:\tools\zig\zig-x86_64-windows-0.14.1\lib\libc\mingw\libsrc\scrnsave.c:193
note: libmingw32.lib(scrnsave.obj):(LaunchScreenSaver)
error: lld-link: undefined symbol: ScreenSaverConfigureDialog
note: referenced by E:\tools\zig\zig-x86_64-windows-0.14.1\lib\libc\mingw\libsrc\scrnsave.c:0
note: libmingw32.lib(scrnsave.obj):(WinMain)
error: lld-link: undefined symbol: __declspec(dllimport) GetStockObject
note: referenced by E:\tools\zig\zig-x86_64-windows-0.14.1\lib\libc\mingw\libsrc\scrnsave.c:193
note: libmingw32.lib(scrnsave.obj):(LaunchScreenSaver)
error: lld-link: undefined symbol: ScreenSaverProc
note: referenced by E:\tools\zig\zig-x86_64-windows-0.14.1\lib\libc\mingw\libsrc\scrnsave.c:291
note: libmingw32.lib(scrnsave.obj):(SysScreenSaverProc)
error: the following command failed with 4 compilation errors:
E:\tools\zig\zig-x86_64-windows-0.14.1\zig.exe build-exe E:\Dev\scratch\clang-zig-link\.zig-cache\o\becf4ae3c77ac4e7ca860206ea1b0af9\foo.lib -ODebug -I E:\Dev\scratch\clang-zig-link\.zig-cache\o\efaed383166e26499e4003483132aafb -lc --cache-dir E:\Dev\scratch\clang-zig-link\.zig-cache --global-cache-dir C:\Users\Reuben\AppData\Local\zig --name main --zig-lib-dir E:\tools\zig\zig-x86_64-windows-0.14.1\lib\ --listen=-
Build Summary: 3/6 steps succeeded; 1 failed
install transitive failure
└─ install main transitive failure
└─ zig build-exe main Debug native 4 errors
error: the following build command failed with exit code 1:
E:\Dev\scratch\clang-zig-link\.zig-cache\o\cd1148d888074864c53ef86518c9dd35\build.exe E:\tools\zig\zig-x86_64-windows-0.14.1\zig.exe E:\tools\zig\zig-x86_64-windows-0.14.1\lib E:\Dev\scratch\clang-zig-link E:\Dev\scratch\clang-zig-link\.zig-cache C:\Users\Reuben\AppData\Local\zig --seed 0xf44808f4 -Z769756041b2fea4b
cl /Zi /std:c11 foo.c /LD /link /out:foo.dll /implib:foo.dll.lib
cl /Zi /std:c11 main.c /Fe:main.exe /link foo.dll.lib
zig build-lib -dynamic -lc foo.c
zig build-exe main.c -lfoo -L. -lc
#include <stdlib.h>
__declspec(dllexport) int foo(int a, int b)
{
return abs(a) ^ b ^ 45678;
}
#include <stdio.h>
__declspec(dllimport) int foo(int a, int b);
int main(void)
{
int d = foo(0x7865, 0x2343);
printf("d: %d\n", d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment