#Basic module questions

1 messages · Page 1 of 1 (latest)

valid prairie
#

I am new here so sorry if I am asking super newb questions. I can't seem to find doc information on how to use modules/namespaces in the official documents. I'm not finding examples that show usage of different files/namespaces besides std.

I used zig init to set up a project and have a foo.zig file with some functions next to main.zig in the src folder. But when I try to use const foo = @import("foo"); the compiler can't find it. I've tried all sorts of paths.

In the unofficial zig.guide they have a thing on modules (https://zig.guide/build-system/modules) but that seems to be for pulling in full libraries and such. Or do I have to modify my build.zig and reorganize my whole project for small files as well?

The Zig build system has the concept of modules, which are other source files

charred carbon
#

if it's a file next to/below main.zig, ie within the src folder, it can be imported with const name = @import("path/to/file.zig");

valid prairie
#

I assumed it would be that simple but here is what I am seeing:

charred carbon
#

const bar = @import("bar.zig");

valid prairie
#

Omg, I could have sworn I had tried that. When reading the @import docs and seeing examples it seemed like I was supposed to leave off the file extension. Maybe that is when you are using the full libraries or modules? Now I feel silly. I warned ya about the newbness lol! ty

ashen orchid
#

Yep. My understanding is any imports without a file extension have to have been set up in the build script (with the exception of "std")

frank parrot
#

@valid prairie @import takes a relative path to a zig file, or an import name.
"Modules" are actually just "libraries" in effect, and you can import then via their name, if you set that up in build.zig with exe.root_module.addImport or the like.

valid prairie
#

In exploring things like raylib I'm seeing in the build.zig.zon config file they provide a github archive url which I imagine I can find but then also a hash. How is that hash created? From this link: https://zig.news/edyu/zig-package-manager-wtf-is-zon-558e it seems to suggest doing this: However, the hash is difficult to find out because it's not just the md5sum, sha1sum, or even sha256sum of the tarball listed in url. The hash does use sha256 but it's not a direct hash of the tarball so it's not easily calculated by the user of the package. but I imagine there might be a more approved solution or way of thinking about this.

Zig NEWS

The power hack and complexity of Package Manager in Zig Ed Yu (@edyu on Github and @edyu on...

uneven sleet
valid prairie
#

exactly what I was looking for, ty!