How to implement conditional compilation in zig?

OK, I think both in this case and in the case of How to set @cImport file path?, you’re trying to use the library as if it was part of the same compilation unit as the executable, instead of just depending on it as a library.

The lib.addOptions() call adds the build_options package to the compilation of src/main.zig and to that one only – when you get to the point of compiling caller.zig you then import the src/main.zig sources and try to access a build_options package which is no longer exposed anywhere, as it was added to lib but not to exe.

In general, if you want src/main.zig to be a library, then you should only use it as a library. Whatever you want to access from the outside, you need to explicitly export, and then you can do exe.linkLibrary(lib) instead of exe.addObjectFile("hardcoded/path/to/lib.a").

See the code example in How to set @cImport file path? for inspiration and try again.

1 Like