`undefined symbol` error when cross-compiling a SDL application from Linux to Windows

I’m trying to build an EXE binary (zig build -Dtarget=x86_64-windows ) of my SDL application, but I’m getting the following error:

error: lld-link: undefined symbol: SDL_main
    note: referenced by /home/labatata/Code/Zig/gui-test/.zig-cache/o/21238e51cec4b55875aa3f4934c63fc2/cimport.zig:5451
    note:               .zig-cache/o/6068ec29821404ac6a69895baaa1c550/gui_test_zcu.obj:(WinMain)
error: the following command failed with 1 compilation errors:
/home/labatata/Downloads/zig-x86_64-linux-0.15.2/zig build-exe .zig-cache/o/33b86cad137ea0e1492002013837a963/SDL3_ttf.lib .zig-cache/o/0f6c66432748752353a
372f9e62e8d9f/clay.lib -ODebug -target x86_64-windows -mcpu baseline -I .zig-cache/o/dd97f4c60629f856cf65ae4bec3fb0b3 -I .zig-cache/o/a6fb089e932f705ef106
5695b36c6e90 --dep gui_test -Mroot=/home/labatata/Code/Zig/gui-test/src/main.zig .zig-cache/o/f8b78e22eccfab8873db452da3e2a81d/SDL3.lib .zig-cache/o/33b86
cad137ea0e1492002013837a963/SDL3_ttf.lib .zig-cache/o/0f6c66432748752353a372f9e62e8d9f/clay.lib -target x86_64-windows -mcpu baseline -I .zig-cache/o/992d
d12464f625030d11fe2c0dc4a395 -I .zig-cache/o/dd97f4c60629f856cf65ae4bec3fb0b3 -I .zig-cache/o/a6fb089e932f705ef1065695b36c6e90 -Mgui_test=/home/labatata/C
ode/Zig/gui-test/src/root.zig .zig-cache/o/f8b78e22eccfab8873db452da3e2a81d/SDL3.lib .zig-cache/o/49e686a5ce59d12a309e90a9fc32e86a/freetype.lib -lkernel32
 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lversion -ladvapi32 -lsetupapi -lshell32 -ldinput8 .zig-cache/o/fca382e571b33c56d3aa0360632bd175/bro
tli.lib -lc --cache-dir .zig-cache --global-cache-dir /home/labatata/.cache/zig --name gui_test --zig-lib-dir /home/labatata/Downloads/zig-x86_64-linux-0.
15.2/lib/ --listen=-

Here’s my build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const mod = b.addModule("gui_test", .{
        .root_source_file = b.path("src/root.zig"),
        .target = target,
    });
    const sdl_dep = b.dependency("sdl", .{
        .target = target,
        .optimize = optimize,
    });
    const sdl_lib = sdl_dep.artifact("SDL3");
    mod.linkLibrary(sdl_lib);

    const sdl_ttf_dep = b.dependency("SDL_ttf", .{
        .target = target,
        .optimize = optimize,
    });
    const sdl_ttf_lib = sdl_ttf_dep.artifact("SDL3_ttf");
    mod.linkLibrary(sdl_ttf_lib);

    const clay_mod = b.createModule(.{
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    clay_mod.addIncludePath(b.path("third-party/clay"));

    const clay_lib = b.addLibrary(.{
        .name = "clay",
        .linkage = .static,
        .root_module = clay_mod,
    });
    clay_lib.addCSourceFile(.{ .file = b.path("third-party/clay/clay_impl.c") });

    mod.linkLibrary(clay_lib);

    const exe = b.addExecutable(.{
        .name = "gui_test",
        // .use_llvm = true,
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "gui_test", .module = mod },
            },
        }),
    });
    exe.linkLibrary(sdl_ttf_lib);
    exe.linkLibrary(clay_lib);

    b.installArtifact(exe);

    const run_step = b.step("run", "Run the app");

    const run_cmd = b.addRunArtifact(exe);
    run_step.dependOn(&run_cmd.step);

    run_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const mod_tests = b.addTest(.{
        .root_module = mod,
    });

    const run_mod_tests = b.addRunArtifact(mod_tests);

    const exe_tests = b.addTest(.{
        .root_module = exe.root_module,
    });

    const run_exe_tests = b.addRunArtifact(exe_tests);

    const test_step = b.step("test", "Run tests");
    test_step.dependOn(&run_mod_tests.step);
    test_step.dependOn(&run_exe_tests.step);
}

What am I missing?

You only link these two to the executable:

exe.linkLibrary(sdl_ttf_lib);
exe.linkLibrary(clay_lib);

maybe this one is missing?

mod.linkLibrary(sdl_lib);

Perhaps this is pertinent:

SDL_main.h will also include platform-specific code (WinMain or whatnot) that calls your actual main function. This is compiled directly into your program.

If for some reason you need to include SDL_main.h in a file but also don’t want it to generate this platform-specific code, you should define a special macro before including the header:

#define SDL_MAIN_NOIMPL

You can do this in your @cImport() statement with @cDefine(), or in the build system with std.Build.Module.addCMacro().

4 Likes