Trying to build a MinGW supported project but getting lld-link: undefined symbol: __declspec(dllimport) _CrtDbgReport

So trying to build this Detours project.

So far their “Makefile” seems to support MinGW my attempts:

fn configDetours(lib: *std.Build.Step.Compile, target: std.Target) !void {
    const exclude_files = [_][]const u8{
        "uimports.cpp",
    };
    const b = lib.step.owner;
    const detoursDir = b.path("Detours/src");
    const path = detoursDir.getPath3(b, &lib.step);
    var dir = try path.openDir("", .{ .iterate = true });
    var dir_iter = dir.iterate();
    defer dir.close();

    outer_loop: while (try dir_iter.next()) |entry| {
        if (entry.kind != .file) continue;
        if (!std.ascii.endsWithIgnoreCase(entry.name, ".cpp")) continue;
        for (exclude_files) |exclude_file| {
            if (std.ascii.eqlIgnoreCase(entry.name, exclude_file)) continue :outer_loop;
        }

        const source_file_path = try detoursDir.join(b.allocator, entry.name);
        lib.addCSourceFile(.{ .file = source_file_path, .language = null, .flags = &.{"-DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x501"} });
    }

    lib.linkLibC();
    if (target.abi != .msvc) {
        lib.linkLibCpp();
    }
}
const detours = b.addStaticLibrary(.{
    .name = "detours",
    .target = target,
    .optimize = optimize,
});
try configDetours(detours, target.result);
// link with others or other stuff

But as from the title I get that undefined symbol error, I am actually not familiar with MinGW that much but I think they got that _CrtDbgReport implemented?

I see zig/lib/libc/include/any-windows-any/crtdbg.h at d238078ae87f1beb565d42caee01ebd6a7a00d43 · ziglang/zig · GitHub has defined? but don’t know much about it either, zig build -Dtarget=x86_64-windows-msvc is fine with the above approach.

_CrtDbgReport is only provided by ucrtbased.dll, which is only linked in Debug mode.

1 Like

umm default zig build mode is Debug right? it’s getting that error when I am doing that, and I am not defining _DEBUG manually, it’s definately being set by the zig compiler why bringing in _DEBUG? Detours/src/detours.cpp at ea6c4ae7f3f1b1772b8a7cda4199230b932f5a50 · microsoft/Detours · GitHub

The Zig compiler defining _DEBUG in Debug mode is by design. It’s to give you a better debug experience out of the box, somewhat matching the experience you get in Zig code.

However, it does seem that something is up with our handling of ucrtbase.dll vs ucrtbased.dll. Would you mind filing an issue on ziglang/zig with a minimal repro? (Should be as simple as calling _CrtDbgReport in a C program.)

4 Likes

Umm hey this would be my first and also would be my first okayish issue to write for zig :slight_smile:

Just wanted to know if the format is okay.

1 Like

Looks good, thanks!

1 Like