#How to build mingw gnu .a file in zig 0.14.1

1 messages · Page 1 of 1 (latest)

neat rock
#

Hello. I’m a beginner in Go and Zig.
I want to statically compile a Zig program into a .a file and use it from Go, but the Zig compilation doesn’t seem to work as expected.
Specifically, even when I set the ABI to gnu, something MSVC-like is generated (I was able to inspect it using dumpbin from Visual Studio).
Since I was able to use the result from C with Visual Studio when setting the ABI to msvc, I don’t think the issue is with my code.
Does anyone have an example of statically building Zig code for MinGW GNU on a Windows 10 environment with Zig 0.14.1?

warm palm
#

you have definitely noticed all the necessary functions as export and callconv(.C)?

neat rock
#

sure
As supporting evidence, the function was successfully called from C using MSVC.
However, using GCC in C results in a linker error.
The same linker error also occurs in Go.

const std = @import("std");

const targets: []const std.Target.Query = &.{
    std.Target.Query{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .msvc },
    std.Target.Query{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu },
    //std.Target.Query{  .cpu_arch = .x86_64,  .os_tag = .linux,    .abi = .gnu,},
};

pub fn build(b: *std.Build) void {
    const optimize = b.standardOptimizeOption(.{});

    for (targets) |t| {
        const resolved_target = b.resolveTargetQuery(t);

        const target_name = std.fmt.allocPrint(
            b.allocator,

            "example-clib-{s}-{s}-{s}",
            .{
                @tagName(t.os_tag.?),
                @tagName(t.cpu_arch.?),
                @tagName(t.abi.?),
            },
        ) catch unreachable;

        const lib = b.addStaticLibrary(.{
            .name = target_name,
            .root_source_file = b.path("src/libmain.zig"),
            .target = resolved_target,
            .optimize = optimize,
        });

        const install_lib = b.addInstallArtifact(lib, .{});
        b.getInstallStep().dependOn(&install_lib.step);
        b.default_step.dependOn(&install_lib.step);
    }
}

this is build.zig

#

this is libmain.zig

pub export fn add_one(x: i32) callconv(.C) i32 {
    return x + 1;
}
#

↓No problem

Dump of file C:\Users\haru\Downloads\example-clib-windows-x86_64-msvc.lib

File Type: LIBRARY

  Summary

        10DB .bss
          A8 .data
       8BFD4 .debug$S
       4076C .debug$T
        2988 .pdata
       35180 .rdata
       67F06 .text
           8 .tls$
        2A08 .xdata

↓ The file can be read by dumpbin, even though it is expected to be incompatible. why

Dump of file C:\Users\haru\Downloads\example-clib-windows-x86_64-gnu.lib

File Type: LIBRARY

  Summary

        10DB .bss
          A8 .data
       8BFD4 .debug$S
       40768 .debug$T
        2988 .pdata
       351E0 .rdata
       67F06 .text
           8 .tls$
        2A08 .xdata

#

I'm not very familiar with this, so I can't say for sure, but this definitely feels wrong.
Could it be that I'm specifying the ABI incorrectly?

warm palm
#

the target looks correct

neat rock
#

ty

#

so this is the bug.right?