How to make Zig automaticly static or dynamic link system library on Windows

as I mention,Zig dosen’t link system library on windows,I must manuly link librarys to make it compile

my build.zig (part)

    exe.root_module.addIncludePath(.{ .cwd_relative = "D:/msys64/clang64/include/" });
    exe.root_module.addLibraryPath(.{ .cwd_relative = "D:/msys64/clang64/lib/" });
    exe.root_module.linkSystemLibrary("sdl3", .{ .use_pkg_config = .yes });
    exe.linkLibC();

this wont compile

I ask LLM,it say’s zig are static link SDL3
but i try’d use dynamic link
exe.linkSystemLibrary2("sdl3", .{ .use_pkg_config = .yes, .preferred_link_mode = .dynamic });
it still wont compile

but if i manuly link all SDL3 needed system library

    exe.linkSystemLibrary("kernel32");
    exe.linkSystemLibrary("user32");
    exe.linkSystemLibrary("gdi32");
    exe.linkSystemLibrary("winmm");
    exe.linkSystemLibrary("imm32");
    exe.linkSystemLibrary("ole32");
    exe.linkSystemLibrary("oleaut32");
    exe.linkSystemLibrary("version");
    exe.linkSystemLibrary("uuid");
    exe.linkSystemLibrary("advapi32");
    exe.linkSystemLibrary("setupapi");
    exe.linkSystemLibrary("shell32");
    exe.linkSystemLibrary("dinput8");

it compiled and work

Just a guess, but try:

exe.linkSystemLibrary2("sdl3", .{
    .preferred_link_mode = .dynamic,
    .search_strategy = .no_fallback,
});

With these flags the linker tries to link only to the dynamic sdl3.
An error must be displayed about a derived name for sdl3 that the linker cannot find.
If a name is not displayed, retry by adding .use_pkg_config = .force,.
Probably the name is different, because zig on windows uses native windows conventions.
Try to locate the expected library file and add its path using addLibraryPath.
If you cannot find the file, locate the smallest sized library that contains only the exported symbol names and add a symbolic link from the expected library name to that file.

1 Like