Open wayjake opened 1 year ago
if u wanna add this to your project do the following:
const std = @import("std");
const builting = @import("builtin");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{
.name = "widow",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
if (builting.os.tag == .windows) {
const win32api = b.createModule(.{
.source_file = .{ .path = "libs/zigwin32/win32.zig" },
});
lib.addModule("win32", win32api);
}
lib.install();
}
As you can see i'm building a static library in this example and depending on the os.tag i'm conditionally including the zigwin32
library, with this i can include it in any zig file in my project using @import("win32");
notice "win32" is the name i specified in the addModule
function.
with this you can successfully build your project with the zig build
command
Thanks for providing these instructions @eddineimad0. Note that these instructions will only work for a limited window of Zig versions, for example 0.11.0-dev.2336+5b82b4004
works but 0.11.0-dev.2619+bd3e248c7
doesn't. Update the line lib.install()
to b.installArtifact(lib)
to work with more recent versions of Zig.
Also in most cases you'll want to add zigwin32
as zig module to whatever artifact you're building instead of wrapping it in a static library and linking it afterwards.
There's also one correction in these instructions, the line if (builtin.os.tag == .windows)
should be if (target.getOs().tag == .windows)
, you want to verify that your compile target is windows rather than checking if the host system building the project is windows.
P.S. I should also mention that soon Zig's package manager should be ready and with that you'll be able to add an entry in build.zig.zon
that will cause zig to automatically download zigwin32 for you, i.e.
build.zig.zon
.{
.name = "myexample",
.version = "0.0.0",
.dependencies = .{
.zigwin32 = .{
.url = "https://github.com/marlersoft/zigwin32/archive/b70e7f818d77a0c0f39b0bd9c549e16439ff5780.tar.gz",
.hash = "TODO PUT CORRECT HASH HERE",
},
},
}
The build.zig
file for this would look something like:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const zigwin32_dep = b.dependency("zigwin32", .{
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "hellowindows",
.root_source_file = .{ .path = "hellowindows.zig" },
.target = target,
.optimize = optimize,
});
exe.addModule("win32", zigwin32_dep);
b.installArtifact(exe);
}
@marler8997 would you mind putting this in the readme file ? it would also help to create releases so we can just use
zig fetch --save <url>
I tried
zig build
and I ended up with this.I am on a Macbook pro (Intel).
Also, this was my first time ever running zig at all. Do I need to use that generator repo first? Let me know if I am missing anything if you can, thanks!