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 do i install it? #61

Open
prajwalch opened this issue Dec 13, 2021 · 17 comments · May be fixed by #125
Open

How do i install it? #61

prajwalch opened this issue Dec 13, 2021 · 17 comments · May be fixed by #125

Comments

@prajwalch
Copy link

prajwalch commented Dec 13, 2021

Hi i am new to zig and i would like to use this library for my project. But I don't see any install instructions on README so idk how can i use it.

@Hejsil
Copy link
Owner

Hejsil commented Dec 13, 2021

Right now, there is no standard way to install Zig libraries. There are a few common ways people do it:

  • git submodule or copying and pasting the library into their own project
    • After this, you can add exe.addPackagePath("clap", "path/to/clap.zig"); to your build.zig file to use the library
  • Use an unofficial package manager such as zigmod or gyro
    • Look at the docs for these package managers for how to install packages using them. They should both be able to install zig-clap as far as I know.

Hope this helps. I'll leave this issue open until Zig has an official package solution. I can then make the choice to add something to the README or not.

@prajwalch
Copy link
Author

prajwalch commented Dec 14, 2021

Right now, there is no standard way to install Zig libraries. There are a few common ways people do it:

  • git submodule or copying and pasting the library into their own project

    • After this, you can add exe.addPackagePath("clap", "path/to/clap.zig"); to your build.zig file to use the library
  • Use an unofficial package manager such as zigmod or gyro

    • Look at the docs for these package managers for how to install packages using them. They should both be able to install zig-clap as far as I know.

Hope this helps. I'll leave this issue open until Zig has an official package solution. I can then make the choice to add something to the README or not.

Thank you.
I decided to use it by using git submodule.

If anyone has a same issue as me here's how you can use it.

  1. On your project root directory make a directory name libs.
  2. Run git submodule add https://github.com/Hejsil/zig-clap libs/zig-clap
  3. Then add exe.addPackagePath("clap", "libs/zig-clap/clap.zig") on your build.zig
  4. Now you can import clap on your source file as const clap = @import("clap");

@punowo
Copy link

punowo commented Jun 5, 2023

Sorry, but currently how do I do this ? exe.addPackagePath didn't seem to work.
Should I just read more on the package manager ?

@prajwalch
Copy link
Author

Sorry, but currently how do I do this ? exe.addPackagePath didn't seem to work. Should I just read more on the package manager ?

This used to work so i am not sure whether this will work or not nowadays but you can try.
Ping me if that doesn't work ;)

exe.addAnonymousModule("zig-clap", .{
    .source_file = .{ .path = "libs/zig-clap/clap.zig" },
});

@punowo
Copy link

punowo commented Jun 5, 2023

exe.addAnonymousModule("zig-clap", .{
    .source_file = .{ .path = "libs/zig-clap/clap.zig" },
});

That works. Thank you.

@Hejsil
Copy link
Owner

Hejsil commented Jun 7, 2023

You can also now try out the zig package manager:

build.zig.zon

.{
    .name = "name-of-my-program-or-package",
    .version = "0.0.0",
    .dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/<git-hash-or-version>.tar.gz",
            .hash = "<the-hash-zig-tells-you-the-package-has>",
        },
    }
}

build.zig

const clap = b.dependecy("clap", .{});
exe.addModule("clap", clap.module("clap"));

Or, if you need multiple things to depend on clap but not use the package manager:

const clap = b.createModule(.{
    .source_file = .{ .path = "lib/zig-clap/clap.zig" },
});
exe.addModule("clap", clap);

@yozachar
Copy link

You can also now try out the zig package manager:

build.zig.zon

.{
    .name = "name-of-my-program-or-package",
    .version = "0.0.0",
    .dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/<git-hash-or-version>.tar.gz",
            .hash = "<the-hash-zig-tells-you-the-package-has>",
        },
    }
}

@Hejsil how do I make zig tell me that?

.{ .name = "themepak", .version = "0.1.0", .dependencies = .{
    .clap = .{
        .url = "https://github.com/Hejsil/zig-clap/archive/34e068756e69e7ee51ba6f27eb9036e19984a128.tar.gz",
        .hash = "288d62e9fb8cbbe5f67c2644d664e3550d8b5a68646c6b32d9881304136f260b",
    },
} }
$ zig build
/home/us-er/Documents/workspace/github/themepak/build.zig.zon:4:17: error: unsupported hash function: only sha2-256 is supported
        .hash = "288d62e9fb8cbbe5f67c2644d664e3550d8b5a68646c6b32d9881304136f260b",
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@yozachar
Copy link

Oh I got it, first comment or remove .hash = ..., line, then run zig build, it'll will then tell you.

$ zig build
Fetch Packages... clap... /home/us-er/Documents/workspace/github/themepak/build.zig.zon:6:20: error: url field is missing corresponding hash field
            .url = "https://github.com/Hejsil/zig-clap/archive/34e068756e69e7ee51ba6f27eb9036e19984a128.tar.gz",
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: expected .hash = "12206795542ab1c19251aac74db20dda4c2abf3289597dd02d52b1d70175064fb298",

@bq-wrongway
Copy link

How did you find this link :
"https://github.com/Hejsil/zig-clap/archive/34e068756e69e7ee51ba6f27eb9036e19984a128.tar.gz",
i just managed to find link from release tags, and they hold number of release after archive/ .

@yozachar
Copy link

yozachar commented Oct 5, 2023

How did you find this link : "https://github.com/Hejsil/zig-clap/archive/34e068756e69e7ee51ba6f27eb9036e19984a128.tar.gz", i just managed to find link from release tags, and they hold number of release after archive/ .

image

Yup that's right.

image

Or from here.

@rawhuul
Copy link

rawhuul commented Jan 9, 2024

You can also now try out the zig package manager:

build.zig.zon

.{
    .name = "name-of-my-program-or-package",
    .version = "0.0.0",
    .dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/<git-hash-or-version>.tar.gz",
            .hash = "<the-hash-zig-tells-you-the-package-has>",
        },
    }
}

build.zig

const clap = b.dependecy("clap", .{});
exe.addModule("clap", clap.module("clap"));

Or, if you need multiple things to depend on clap but not use the package manager:

const clap = b.createModule(.{
    .source_file = .{ .path = "lib/zig-clap/clap.zig" },
});
exe.addModule("clap", clap);

image

swats on  main [!] via ↯ v0.12.0-dev.2063+804cee3b9 ❯ zig version
0.12.0-dev.2063+804cee3b9
swats on  main [!] via ↯ v0.12.0-dev.2063+804cee3b9 ❯

How to resolve this issue?

@Hejsil
Copy link
Owner

Hejsil commented Jan 9, 2024

@rawhuul Version 0.7 of zig-clap targets 0.11 of zig. There is no guarantee that this version will work with newer version of zig. Seems you're very close to zig master, so you should get a master version of zig clap as well.

@rawhuul
Copy link

rawhuul commented Jan 9, 2024

@rawhuul Version 0.7 of zig-clap targets 0.11 of zig. There is no guarantee that this version will work with newer version of zig. Seems you're very close to zig master, so you should get a master version of zig clap as well.

OKay I will try with master version

@MathewDeyo
Copy link

Any insight on using the Zig Package Manager?

$ zig version
0.12.0-dev.3212+40e64245f

build.zig.zon:

.{
    .name = "package-name-here",
    .version = "0.0.0",
    .dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/refs/heads/master.tar.gz",
            .hash = "12203896de6eedec14712f4f1eaac8b646939cfed213c56accf231a0abb05f9dbb77",
         },
    },
    .paths = .{
        "",
    },
}

build.zig:

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));
exe.linkLibrary(clap.artifact("clap"));

And when I try to zig build:

$ zig build
thread 39008 panic: unable to find artifact 'clap'
C:\Users\user\.zvm\master\lib\std\debug.zig:434:22: 0x9338f4 in panicExtra__anon_18765 (build.exe.obj)
    std.builtin.panic(msg, trace, ret_addr);
                     ^
C:\Users\user\.zvm\master\lib\std\debug.zig:409:15: 0x908bac in panic__anon_17759 (build.exe.obj)
    panicExtra(null, null, format, args);
              ^
C:\Users\user\.zvm\master\lib\std\Build.zig:1790:18: 0x8dab74 in artifact (build.exe.obj)
            panic("unable to find artifact '{s}'", .{name});
                 ^
C:\Users\user\workspace\build.zig:28:34: 0x88f656 in build (build.exe.obj)
    exe.linkLibrary(clap.artifact("clap"));
                                 ^
C:\Users\user\.zvm\master\lib\std\Build.zig:1992:33: 0x86dfa5 in runBuild__anon_8716 (build.exe.obj)
        .Void => build_zig.build(b),
                                ^
C:\Users\user\.zvm\master\lib\build_runner.zig:310:29: 0x868f5a in main (build.exe.obj)
        try builder.runBuild(root);
                            ^
C:\Users\user\.zvm\master\lib\std\start.zig:350:53: 0x86ffdc in WinStartup (build.exe.obj)
    std.os.windows.ntdll.RtlExitUserProcess(callMain());
                                                    ^
???:?:?: 0x7ff80d99257c in ??? (KERNEL32.DLL)
???:?:?: 0x7ff80f0eaa57 in ??? (ntdll.dll)
error: unable to read results of configure phase from 'C:\Users\user\workspace\zig-cache\tmp\ba9a75b7430cf0ff': FileNotFound

If I remove exe.linkLibrary(clap.artifact("clap"));, the build works, but I can't run the application:

$ zig run src/main.zig -- -h
src\main.zig:2:22: error: no module named 'clap' available within module main
const clap = @import("clap");
                     ^~~~~~
referenced by:
    main: src\main.zig:13:29
    callMain: C:\Users\user\.zvm\master\lib\std\start.zig:511:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

I've tried using Zig 0.11.0 with tag 0.7.0 as well (and converted the build.zig file to match) with basically the same results:

$ zig run src/main.zig -- -h
src\main.zig:2:22: error: no package named 'clap' available within package 'root'
const clap = @import("clap");
                     ^~~~~~
referenced by:
    main: src\main.zig:13:29
    callMain: C:\Users\user\.zvm\bin\lib\std\start.zig:574:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

@Hejsil
Copy link
Owner

Hejsil commented Mar 11, 2024

@MathewDeyo

You have the right code in build.zig after you removed exe.linkLibrary(clap.artifact("clap"));. The problem you're hitting now is that zig run doesn't use the build system. Only zig build actually looks at build.zig. I don't think this issue is a place for a zig tutorial so please ask in any of the zig communities for help and advice :)

@kothavade
Copy link

In Zig 0.12 it seems like the way to do this is now (new to Zig, so this may be wrong, but it's working for me):

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));

@janie314
Copy link

janie314 commented Jun 3, 2024

To summarize:

(1) Run this:

zig fetch --save git+https://github.com/Hejsil/zig-clap

(2) Then add @kothavade 's suggestion in build.zig:

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));

Yeag this sould probably be improved.

Happy Zigging.

@janie314 janie314 linked a pull request Jun 3, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

9 participants