#Conditional build tags

1 messages · Page 1 of 1 (latest)

astral grotto
kindred blaze
#

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

astral grotto
kindred blaze
#

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

astral grotto
kindred blaze
#

generally you just define it as an export

#

e.g. on linux, pub export fn _start() noreturn {...}

kindred blaze
#

the same type of code would exist for Go or C

#

it's just it's all visible in the zig stdlib

astral grotto
kindred blaze
#

you can continue to use pub fn main as normal

#

start.zig is a file in the stdlib

wheat jackal