In Golang, there is a build tag in the form of:
// +build tag_name
Is there something similar in zig?
https://www.digitalocean.com/community/tutorials/customizing-go-binaries-with-build-tags
1 messages · Page 1 of 1 (latest)
In Golang, there is a build tag in the form of:
// +build tag_name
Is there something similar in zig?
https://www.digitalocean.com/community/tutorials/customizing-go-binaries-with-build-tags
not really similar
but you can achieve the same effect via different mechanisms
in zig, all code is lazily analysed
so you can have a file, imported and everything
but so long as you dont reference the code, it's never analysed, and therefore never compiled
this means you can do things like
fn foo() void {
if (comptime_bool_constant) {
fooImpl1();
} else {
fooImpl2();
}
}
if comptime_bool_constant is true, fooImpl1() will be analysed and compiled, and fooImpl2() won't
and vice versa if it's false
Thanks for the exposition, this makes me understand now why I didn't get any error about having the function main() defined in an imported file!
well, that's actually moreso to do with the fact that main isn't specially recognised by the language or anything
there's a file called start.zig, where all of the platform-specific code for setting up the entry point is
that imports @import("root"), which will be the file you passed to zig build-exe, or in build.zig the file you passed as the root_source_file
it checks if you have a public main function via @hasDecl(root, "main")
if you do, then it runs your main function from the platform-specific entry point, which is just an @exported function
if you don't, it then checks if you're defining a platform-specific entry point yourself
if you're not doing that either, it'll issue a compile error about missing a main entry point
mm, I thought I needed to define the file directly as zig run main.zig and in this one I have to use the main function, any reference how I can define platform-dpecific entry points.
generally you just define it as an export
e.g. on linux, pub export fn _start() noreturn {...}
to be clear it's just the code that sets up stuff for being able to just have pub fn main() T {}, instead of doing it all yourself
the same type of code would exist for Go or C
it's just it's all visible in the zig stdlib
so do you mean pub export fn _start() is equivalent to Go 'func main()`, sorry I got confused, is it the file name to be start.zig and inside it an exported function regardless of its name, or it is the external _start function regardless if the file name, or both, in Go the filename is not a big deal.
no, I'm just referencing an implementation detail
you can continue to use pub fn main as normal
start.zig is a file in the stdlib
As mentioned, main is just something the language provides so you don't have to think about platform-specific details…
The reason you can have more than one main() function in Zig but not in Go is because in Zig each file has its own namespace, whereas in Go the namespace extends across the entire directory (tho not sub directories?)