5

I'm trying to link a zig static library with golang. here is my build.zig

const std = @import("std");

pub fn build(b: *std.Build) void {
    const windows = b.option(bool, "windows", "Target Microsoft Windows") orelse false;
    const arm64 = b.option(bool, "arm64", "Target ARM64 architecture") orelse false;

    const target = b.resolveTargetQuery(.{
        .cpu_arch = if (arm64) .aarch64 else if (windows) .x86_64 else null,
        .os_tag = if (windows) .windows else null,
    });
    const optimize = b.standardOptimizeOption(.{});

    const zigwin32 = b.dependency("zigwin32", .{});

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

    lib.root_module.addImport("zigwin32", zigwin32.module("win32"));

    b.installArtifact(lib);

    const exe = b.addExecutable(.{
        .name = "myapp",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("zigwin32", zigwin32.module("win32"));
    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }
    b.step("run", "Run the app").dependOn(&run_cmd.step);
}

I want to use this in my go program with the following #cgo LDFLAGS: -L./zig1/zig-out/lib -lmyziglib

I compile Zig with - zig build -Dwindows=true -Darm64=true

I compile then golang with - GOOS=windows GOARCH=arm64 CGO_ENABLED=1 \ CC="zig cc -target aarch64-windows-gnu" \ go build -o main.exe .

I get the following errors -

lld-link: error: duplicate symbol: _tls_end
>>> defined at myziglib.lib(myziglib.lib.obj)
>>> defined at libmingw32.lib(tlssup.obj)

lld-link: error: duplicate symbol: __xl_a
>>> defined at myziglib.lib(myziglib.lib.obj)
>>> defined at libmingw32.lib(tlssup.obj)

lld-link: error: duplicate symbol: __xl_z
>>> defined at myziglib.lib(myziglib.lib.obj)
>>> defined at libmingw32.lib(tlssup.obj)

lld-link: error: duplicate symbol: _tls_used

>>> defined at myziglib.lib(myziglib.lib.obj)
>>> defined at libmingw32.lib(tlssup.obj)

Any ideas? I was able to link go and zig with a simple program. but I soon as i started using win32 I started to get this error

1
  • tlssup.obj appears to be a part of the standard C library (libc.lib probably), and supposedly it gets statically linked to your zig lib. Then MinGW's linker tries to link the resulting Go program, it apparently links it with libmingw32.lib which includes the same symbols as well — producing the conflict. Maybe if you'll try to link the zig lib w/o statically linking libc (leaving its symbols undefined there) it will work. Commented Aug 18 at 22:50

1 Answer 1

3

Fixed with adding

lib.linkLibC();

to build.zig
and linking sys libraries in cgo

#cgo LDFLAGS: -L./zig1/zig-out/lib -lmyziglib -lsetupapi -ladvapi32 -lole32 -loleaut32 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.