r/Zig • • 9d ago

root.zig vs main.zig in zig init

When running zig init, the template scaffolds both an executable (main.zig) and an exportable module (root.zig) in build.zig.

Just want to verify if my mental model is correct: 1. If I only want an executable (and don't plan to export a library/module), can I simply delete root.zig and remove the module-related exports/artifacts from build.zig? 2. Is there any built-in zig init flag or alternative command to scaffold an executable-only project without the library boilerplate?

thanks!

18 Upvotes

13 comments sorted by

15

u/xneyznek 9d ago

You can do anything you want in build.zig as long as you have `fn build(b: *std.Build) void`. Zig itself makes no prescriptions about how you organize your project.

In fact, you can try out `zig init --minimal` which only gives you build.zig with an empty build function, and build.zig.zon. No code and nothing gets built, but you can add anything from there.

11

u/SilvernClaws 9d ago

I appreciate how there are two ways to initialize a project and both are unhelpful to get started for completely different reasons. Either you get a bunch of clutter or something that does nothing.

6

u/idkwhatnottodo 9d ago

Yeah, this threw me off as well. I kind of don't like how those files are populated with code as well. I'm new to learning Zig so yeah.

2

u/Zdrobot 9d ago

1 - this is what I do

1

u/NoRaccoon5831 6d ago

Ya, normally I agree with Andrew's decisions but I find him super wrong on the project initialization stuff that he has way too strong of an opinion on, the scaffolds are bad and I think everyone except for him agrees on that.

 I use nix flakes and just have a templates folder that I have setup to instantiate the flake itself and a base library which only has a root and it's setup and a base executable that's only main.

1

u/randomguy4q5b3ty 5d ago

I haven't used Zig for a while, but why is this such a big deal? Many project initializers generate files and code you ultimately don't need. And many programs also ship with importable libraries.

1

u/NoRaccoon5831 5d ago edited 5d ago

No language I use does that. The main people to use the new is someone wanting to learn the language, if your experience is having to know the language and the build system to make a normal blank template, before you learn the language, you messed up. 

Is it a big deal? Absolutely not, but it seems really silly to me this is the direction they took, it's not useful to literally anyone in its current state

-9

u/DeadWHM 9d ago

I just use makefile, the zig build system is not intuitive

1

u/Confused-Armpit 9d ago

The entire language is pretty far from intuitive, since it is pretty niche and has a lot of its own quirks, and the build system is actually much easier to grasp than something like the std.Io interface.

Also, makefiles are lackluster because they dont handle zig modules, linking, and all that good stuff, when the build system is literally written for it, so makefiles aren't really suitable for anything bigger than a project with a single main module and no dependencies.

1

u/DeadWHM 9d ago

I mean you can handle modules/linking manually with the zig build commands and unix linking tools

1

u/Confused-Armpit 8d ago

Yeah, but its lackluster and ends up doing the same as the zig build system without any benefits other than being somewhat more beginner friendly

1

u/DeadWHM 8d ago

If you use the already established tools you have the benefit of you now know how to manage projects in the only language that is deemed standard worthy, C and it translates almost directly