I am on an x86_64-windows
machine and I am trying to target x86_64-windows-msvc
.
My project downloads some DLLs and tries to link against them using the zig build system.
The DLLs target the MSVC ABI.
However, when I run
zig build -Dtarget=native-native-msvc
I get linker errors for lots of undefined symbols:
error: lld-link: undefined symbol: __declspec(dllimport) closesocket
note: referenced by zenohc.lib(zenohc.zenohc.87d05d4354ee0cf2-cgu.0.rcgu.o):(zenoh_runtime::ZRuntime::block_in_place::_$u7b$$u7b$closure$u7d$$u7d$::hee46949cbf4edbe3)
error: lld-link: undefined symbol: __declspec(dllimport) setsockopt
note: referenced by zenohc.lib(zenohc.zenohc.87d05d4354ee0cf2-cgu.0.rcgu.o):(zenoh::net::runtime::orchestrator::_$LT$impl$u20$zenoh..net..runtime..Runtime$GT$::start_scout::_$u7b$$u7b$closure$u7d$$u7d$::h5deafa0657b41f30)
note: referenced by zenohc.lib(zenohc.zenohc.87d05d4354ee0cf2-cgu.0.rcgu.o):(zenoh::net::runtime::orchestrator::_$LT$impl$u20$zenoh..net..runtime..Runtime$GT$::start_scout::_$u7b$$u7b$closure$u7d$$u7d$::h5deafa0657b41f30)
note: referenced by zenohc.lib(zenohc.zenohc.87d05d4354ee0cf2-cgu.0.rcgu.o):(zenoh::net::runtime::orchestrator::_$LT$impl$u20$zenoh..net..runtime..Runtime$GT$::start_scout::_$u7b$$u7b$closure$u7d$$u7d$::h5deafa0657b41f30)
note: referenced 25 more times
I’m not sure why I am getting linker errors. I am linking libc in my build.zig:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// expose a public module called "zenoh"
const zenoh = b.addModule("zenoh", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
// download the correct pre-compiled zenoh static library
const zenoh_c_dep = switch (target.result.cpu.arch) {
.x86_64 => switch (target.result.os.tag) {
.windows => switch (target.result.abi) {
.gnu => b.lazyDependency("zenoh_c_x86_64_windows_gnu", .{}),
.msvc => b.lazyDependency("zenoh_c_x86_64_windows_msvc", .{}),
else => @panic("unsupported target"),
},
.linux => switch (target.result.abi) {
.musl => b.lazyDependency("zenoh_c_x86_64_linux_musl", .{}),
.gnu => b.lazyDependency("zenoh_c_x86_64_linux_gnu", .{}),
else => @panic("unsupported target"),
},
.macos => switch (target.result.abi) {
.none => b.lazyDependency("zenoh_c_x86_64_macos_none", .{}),
else => @panic("unsupported target"),
},
else => @panic("unsupported target"),
},
.aarch64 => switch (target.result.os.tag) {
.linux => switch (target.result.abi) {
.musl => b.lazyDependency("zenoh_c_aarch64_linux_musl", .{}),
.gnu => b.lazyDependency("zenoh_c_aarch64_linux_gnu", .{}),
else => @panic("unsupported target"),
},
.macos => switch (target.result.abi) {
.none => b.lazyDependency("zenoh_c_aarch64_macos_none", .{}),
else => @panic("unsupported target"),
},
else => @panic("unsupported target"),
},
else => @panic("unsupported target"),
} orelse return;
const zenoh_c_static_lib_path = switch (target.result.os.tag) {
.linux, .macos => zenoh_c_dep.path("lib/libzenohc.a"),
.windows => switch (target.result.abi) {
.msvc => zenoh_c_dep.path("lib/zenohc.lib"),
.gnu => zenoh_c_dep.path("lib/libzenohc.a"),
else => @panic("unsupported target"),
},
else => @panic("unsupported target"),
};
// expose the functions in the header to zig using translate-c
const translate_c = b.addTranslateC(.{
.link_libc = true,
.optimize = optimize,
.target = target,
.root_source_file = zenoh_c_dep.path("include/zenoh.h"),
});
zenoh.addImport("zenoh_c", translate_c.createModule());
// link the zenoh static library to zig
zenoh.addObjectFile(zenoh_c_static_lib_path);
// run some unit tests to sanity check
const lib_unit_tests = b.addTest(.{
.root_module = zenoh,
});
lib_unit_tests.linkLibC();
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
// examples
const examples_tests = b.addTest(.{
.root_source_file = b.path("examples/root.zig"),
.target = target,
.optimize = optimize,
});
examples_tests.linkLibC();
examples_tests.root_module.addImport("zenoh", zenoh);
const run_examples_tests = b.addRunArtifact(examples_tests);
const examples_step = b.step("examples", "Run the examples.");
examples_step.dependOn(&run_examples_tests.step);
// make the default step run the tests
b.default_step.dependOn(test_step);
}
What am I doing wrong here? Do I need to install visual studio or something?