Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a static binary, or static test? #427

Open
TwoClocks opened this issue Jan 28, 2025 · 1 comment
Open

How to create a static binary, or static test? #427

TwoClocks opened this issue Jan 28, 2025 · 1 comment

Comments

@TwoClocks
Copy link

It's very unclear to me how to create statically linked output for either binaries or tests.

There is a "linker_script" param, but I can't see any examples using it, or what the format of the param or "script" should be.

Thanks in advance.

@aherrmann
Copy link
Owner

That's controlled via the target platform. A pure Zig binary on Linux should already be statically linked. But, if it depends on a libc, then it depends on which libc you choose. The default is glibc, in which case the binary will be dynamically linked. Using musl instead will yield a static binary.

Example platform configurations can be found here.
The following should yield a statically linked binary using musl with bazel build :static:

platform(
    name = "x86_64-linux-musl",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
        "@rules_zig//zig/platforms/abi:musl",
    ],
)

zig_binary(
    name = "unconfigured",
    main = "main.zig",
    # ...
    tags = ["manual"],
)

zig_configure_binary(
    name = "static",
    actual = ":unconfigured",
    target = ":x86_64-linux-musl",
)

The same applies to test targets.

The linker-script attribute should not be needed here. But, an example can be found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants