| ... | ... | @@ -8550,7 +8550,8 @@ fn foo(comptime T: type, ptr: *T) T { |
| 8550 | 8550 | {#header_open|opaque#} |
| 8551 | 8551 | <p> |
| 8552 | 8552 | {#syntax#}opaque {}{#endsyntax#} declares a new type with an unknown (but non-zero) size and alignment. |
| 8553 | | It can have declarations like structs, unions, or enums. |
| 8553 | It can contain declarations the same as {#link|structs|struct#}, {#link|unions|union#}, |
| 8554 | and {#link|enums|enum#}. |
| 8554 | 8555 | </p> |
| 8555 | 8556 | <p> |
| 8556 | 8557 | This is typically used for type safety when interacting with C code that does not expose struct details. |
| ... | ... | @@ -9626,9 +9627,99 @@ test "assert in release fast mode" { |
| 9626 | 9627 | </p> |
| 9627 | 9628 | {#header_close#} |
| 9628 | 9629 | {#header_open|Zig Build System#} |
| 9629 | | <p>TODO: explain purpose, it's supposed to replace make/cmake</p> |
| 9630 | | <p>TODO: example of building a zig executable</p> |
| 9631 | | <p>TODO: example of building a C library</p> |
| 9630 | |
| 9631 | <p>Simple programs can be built with {#syntax#}zig |
| 9632 | build-exe{#endsyntax#} and {#syntax#}zig build-lib{#endsyntax#}, |
| 9633 | but running those commands manually gets tedious and error |
| 9634 | prone. Zig's build system lets you keep all the command line |
| 9635 | switches and build modes in one place. It has no external |
| 9636 | dependencies, so Zig code can be built on any platform without |
| 9637 | installing more programs.</p> |
| 9638 | <p>To use the build system, run |
| 9639 | <code class="shell">$ zig build [command]</code> |
| 9640 | where {#syntax#}[command]{#endsyntax#} is an optional target, |
| 9641 | configured by your build.zig file. There is more detail |
| 9642 | on <a href="https://github.com/ziglang/zig/wiki/Zig-Build-System">the |
| 9643 | wiki</a> but here are some example build.zig files to get you |
| 9644 | started:</p> |
| 9645 | |
| 9646 | {#header_open|Building a Zig Executable#} |
| 9647 | <p>This <code>build.zig</code> file is automatically generated |
| 9648 | by <code>zig init-exe</code>.</p> |
| 9649 | {#code_begin|syntax|build#} |
| 9650 | const Builder = @import("std").build.Builder; |
| 9651 | |
| 9652 | pub fn build(b: *Builder) void { |
| 9653 | // Standard target options allows the person running `zig build` to choose |
| 9654 | // what target to build for. Here we do not override the defaults, which |
| 9655 | // means any target is allowed, and the default is native. Other options |
| 9656 | // for restricting supported target set are available. |
| 9657 | const target = b.standardTargetOptions(.{}); |
| 9658 | |
| 9659 | // Standard release options allow the person running `zig build` to select |
| 9660 | // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. |
| 9661 | const mode = b.standardReleaseOptions(); |
| 9662 | |
| 9663 | // This line tells the Zig build system where to find the file |
| 9664 | // that contains main and what to call the executable. |
| 9665 | const exe = b.addExecutable("main", "src/main.zig"); |
| 9666 | exe.setTarget(target); |
| 9667 | exe.setBuildMode(mode); |
| 9668 | exe.install(); |
| 9669 | |
| 9670 | const run_cmd = exe.run(); |
| 9671 | run_cmd.step.dependOn(b.getInstallStep()); |
| 9672 | |
| 9673 | // This will be executed by "zig build run" |
| 9674 | const run_step = b.step("run", "Run the app"); |
| 9675 | run_step.dependOn(&run_cmd.step); |
| 9676 | } |
| 9677 | {#code_end#}{#header_close#} |
| 9678 | |
| 9679 | {#header_open|Building a C library#} |
| 9680 | {#code_begin|syntax#} |
| 9681 | const Builder = @import("std").build.Builder; |
| 9682 | |
| 9683 | pub fn build(b: *Builder) void { |
| 9684 | const mode = b.standardReleaseOptions(); |
| 9685 | // Add a target that generates libbadmath, with no Zig source files. |
| 9686 | const lib = b.addStaticLibrary("badmath", null); |
| 9687 | lib.setBuildMode(mode); |
| 9688 | // This particular library exists entirely in src/lib.c. |
| 9689 | lib.addCSourceFile("src/lib.c", &[_][]const u8{ |
| 9690 | "-Wall", |
| 9691 | "-Wextra", |
| 9692 | "-Werror", |
| 9693 | }); |
| 9694 | // libbadmath.a will be put in this directory, instead of only |
| 9695 | // living in zig-cache. |
| 9696 | lib.setOutputDir("obj"); |
| 9697 | lib.install(); |
| 9698 | } |
| 9699 | {#code_end#} |
| 9700 | {#header_close#} |
| 9701 | |
| 9702 | {#header_open|Extending a C library#} |
| 9703 | {#code_begin|syntax#} |
| 9704 | const Builder = @import("std").build.Builder; |
| 9705 | |
| 9706 | pub fn build(b: *Builder) void { |
| 9707 | const mode = b.standardReleaseOptions(); |
| 9708 | // This line tells the build system to make a static library |
| 9709 | // called "add" using source from "src/main.zig". |
| 9710 | const lib = b.addStaticLibrary("add", "src/main.zig"); |
| 9711 | lib.setBuildMode(mode); |
| 9712 | lib.force_pic = true; |
| 9713 | // Include the compiler's runtime environment in the static library. |
| 9714 | lib.bundle_compiler_rt = true; |
| 9715 | |
| 9716 | var main_tests = b.addTest("src/main.zig"); |
| 9717 | main_tests.setBuildMode(mode); |
| 9718 | |
| 9719 | const test_step = b.step("test", "Run library tests"); |
| 9720 | test_step.dependOn(&main_tests.step); |
| 9721 | } |
| 9722 | {#code_end#}{#header_close#} |
| 9632 | 9723 | {#header_close#} |
| 9633 | 9724 | {#header_open|C#} |
| 9634 | 9725 | <p> |