如何在Zig中进行条件编译?

huangapple go评论140阅读模式
英文:

How to do conditional compilation with Zig?

问题

例如,我可以使用CMake为C/C++预处理器添加定义

  1. add_definitions(-DFOO -DBAR ...)

然后我可以在条件编译中使用它们

  1. #ifdef FOO
  2. 代码 ...
  3. #endif
  4. #ifdef BAR
  5. 代码 ...
  6. #endif

是否可以通过编译参数或类似的方式在Zig及其构建系统中执行相同的操作?

英文:

For example, I can add definitions for C/C++ preprocessor with CMake

  1. add_definitions(-DFOO -DBAR ...)

and then I can use them for conditional compilation

  1. #ifdef FOO
  2. code ...
  3. #endif
  4. #ifdef BAR
  5. code ...
  6. #endif

Is there a way to do the same thing with Zig and its build system using compilation arguments or something like that?

答案1

得分: 3

以下是翻译好的部分:

  1. 创建一个名为build.zig的单独文件,其中包含一个名为build()的函数:
  1. const std = @import("std");
  2. pub fn build(b: *std.build.Builder) !void {
  3. const build_options = b.addOptions();
  4. // 添加命令行标志
  5. // 并设置默认值
  6. build_options.addOption(bool, "sideways", b.option(bool, "sideways", "print sideways") orelse false);
  7. // 设置可执行文件名和源代码
  8. const exe = b.addExecutable("hello", "hello.zig");
  9. exe.addOptions("build_options", build_options);
  10. // 编译并复制到 zig-out/bin
  11. exe.install();
  12. }
  1. 在名为hello.zig的单独文件中使用条件编译选项,使用@import("build_options")
  1. const std = @import("std");
  2. pub fn main() !void {
  3. const print_sideways = @import("build_options").sideways;
  4. const stdout = std.io.getStdOut().writer();
  5. if (print_sideways) {
  6. try stdout.print("Sideways Hello, {s}!\n", .{"world"});
  7. } else {
  8. try stdout.print("Regular Hello, {s}!\n", .{"world"});
  9. }
  10. }
  1. 使用以下命令进行编译:
  1. zig build -Dsideways=true
  1. 执行zig-out/bin/hello将输出以下内容:
  1. Sideways Hello, world!
英文:

You can do something similar using the build system. This requires some boilerplate code to do the option handling. Following the tutorial on https://zig.news/xq/zig-build-explained-part-1-59lf for the build system and https://ziggit.dev/t/custom-build-options/138/8 for the option handling:

  1. Create a separate file called build.zig that contains a function build():
  1. const std = @import("std");
  2. pub fn build(b: *std.build.Builder) !void {
  3. const build_options = b.addOptions();
  4. // add command line flag
  5. // and set default value
  6. build_options.addOption(bool, "sideways", b.option(bool, "sideways", "print sideways") orelse false);
  7. // set executable name and source code
  8. const exe = b.addExecutable("hello", "hello.zig");
  9. exe.addOptions("build_options", build_options);
  10. // compile and copy to zig-out/bin
  11. exe.install();
  12. }
  1. Use the option for conditional compilation in a separate file hello.zig using @import("build_options"):
  1. const std = @import("std");
  2. pub fn main() !void {
  3. const print_sideways = @import("build_options").sideways;
  4. const stdout = std.io.getStdOut().writer();
  5. if (print_sideways) {
  6. try stdout.print("Sideways Hello, {s}!\n", .{"world"});
  7. } else {
  8. try stdout.print("Regular Hello, {s}!\n", .{"world"});
  9. }
  10. }
  1. Compile with:
  1. zig build -Dsideways=true
  1. Executing zig-out/bin/hello gives the following output:
  1. Sideways Hello, world!

huangapple
  • 本文由 发表于 2023年6月2日 02:22:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定