If I set the target to x86_64-windows-gnu
it works.
However, if I set it to x86_64-windows-msvc
it fails to link.
error: lld-link: .zig-cache\o\320d857c0552b55ee2db1d4ddda36f58\hello.dll: bad file type. Did you specify a DLL instead of an import library?
I think this is because zig is not passing the /lldmingw
flag to the lld linker.
b.addLibrary
with .linkage = .dynamic
produces dll with import library .lib
file so I tried using exe.linkLibrary
instead of exe.linkSystemLibrary
, but it still didn’t work.
error: warning(link): unexpected LLD stderr:
lld-link: warning: undefined symbol: hello
My project setup:
// src/hello.c
#include <stdio.h>
void hello() {
puts("hello");
}
// src/main.zig
const hello = struct {
extern "hello" fn hello() void;
};
pub fn main() void {
hello.hello();
}
// build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "learn_zig",
.root_module = exe_mod,
});
b.installArtifact(exe);
const hello_mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
const hello = b.addLibrary(.{
.name = "hello",
.root_module = hello_mod,
.linkage = .dynamic,
});
hello.addCSourceFiles(.{
.files = &.{"src/hello.c"},
.flags = &.{},
});
hello.linkLibC();
b.installArtifact(hello);
exe.step.dependOn(&hello.step);
exe.addLibraryPath(hello.getEmittedBinDirectory());
exe.linkSystemLibrary("hello"); // exe.linkLibrary(hello);
// ...
}