Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get it up and running #18

Open
wayjake opened this issue May 5, 2023 · 4 comments
Open

How to get it up and running #18

wayjake opened this issue May 5, 2023 · 4 comments

Comments

@wayjake
Copy link

wayjake commented May 5, 2023

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
Copy link

eddineimad0 commented May 10, 2023

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
Copy link
Contributor

marler8997 commented May 11, 2023

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
Copy link
Contributor

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
Copy link

yvz5 commented Dec 29, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants