marlersoft / zigwin32

Zig bindings for Win32 generated by https://github.com/marlersoft/zigwin32gen
MIT License
234 stars 30 forks source link

How to get it up and running #18

Open wayjake opened 1 year ago

wayjake commented 1 year ago

I tried zig build and I ended up with this.

/Users/username/devops/zigwin32/build.zig:3:21: error: root struct of file 'std' has no member named 'Build'
pub fn build(b: *std.Build) void {
                 ~~~^~~~~~
referenced by:
    runBuild: /usr/local/Cellar/zig/0.10.1/lib/zig/build_runner.zig:231:45
    usage__anon_4562: /usr/local/Cellar/zig/0.10.1/lib/zig/build_runner.zig:242:13
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

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!

eddineimad0 commented 1 year ago

if u wanna add this to your project do the following:

  1. create the project with zig init.
  2. create a libs folder in the root of the project (this is by convention where you place the external libraries you wanna use).
  3. clone the zigwin32 repo inside that libs folder (you can also use other strategies like git submodule for cloning the repo).
  4. inside your project's build.zig specify the zigwin32 as a module here is an example of mine:
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

marler8997 commented 1 year ago

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.

marler8997 commented 1 year ago

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);
}
yvz5 commented 6 months ago

@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>