Defining a comptime value via a zig build-exe option

i’m looking for the equivalent of -D, except i’m interested in having my own comptime value that i test in my code… i want to do the moral equivalent of #ifdef in mysource code…

i already see lots of build-time info in std.builtin; i just want to add my own build-time parameter value…

note that i’m using zig build-exe (as opposed to zig build)…

There is no way of doing this with zig build-exe.

zig build has b.addOptions(...), which can be used to pass build-time constants to the source code being compiled and is described in more detail in the build system docs, but all it really does is generate regular Zig code that is subsequently added as a regular module import. For zig build-exe invocations outside of a build.zig script, whatever you are using to orchestrate the compilation of the application would need to generate this code in advance and have your main program import it appropriately.

For advanced use cases like these you are strongly encouraged to just use build zig – you don’t necessarily have to replace your entire build system; you could just do the Zig-specific tasks with a build.zig and leave the rest as is.

2 Likes