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

make --static only apply to libraries. link exes with static flag when all libraries are static. #1828

Closed
gernest opened this issue Dec 13, 2018 · 3 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@gernest
Copy link

gernest commented Dec 13, 2018

I'm using macos

$ zig version
0.3.0+634d11ab

I created a simple zig program from zig init-exe without any modification. Only updated build.zig to addStaticExecutable.

Full build.zig

const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
    const mode = b.standardReleaseOptions();
    const exe = b.addStaticExecutable("static_bin", "src/main.zig");
    exe.setBuildMode(mode);

    const run_step = b.step("run", "Run the app");
    const run_cmd = b.addCommand(".", b.env_map, [][]const u8{exe.getOutputPath()});
    run_step.dependOn(&run_cmd.step);
    run_cmd.step.dependOn(&exe.step);

    b.default_step.dependOn(&exe.step);
    b.installArtifact(exe);
}

I buiild zig from source

this is what I get when running zig build

$ zig build
lld: error: Unable to find library for -lcrt0.o

The following command exited with error code 1:
/Volumes/code/zig/zig-src/build/bin/zig build-exe /Volumes/code/projects/static_bin/src/main.zig --cache-dir /Volumes/code/projects/static_bin/zig-cache --output /Volumes/code/projects/static_bin/zig-cache/static_bin --name static_bin --static

Build failed. The following command failed:
/Users/g.ernest/Library/Application Support/zig/stage1/artifact/XnaMzL5v9s474uerZy7ikLO3pLp1uXTHKR3RfyZEdDzTvlhJt65NT14QPxOVaKjU/build /Volumes/code/zig/zig-src/build/bin/zig /Volumes/code/projects/static_bin /Volumes/code/projects/static_bin/zig-cache

Am I missing something?

@andrewrk andrewrk added this to the 0.4.0 milestone Dec 13, 2018
@andrewrk
Copy link
Member

andrewrk commented Dec 13, 2018

I think we need to rework what "static" means in the context of executables. I started off by using the same concept as exists in C compilers, but then drifted away as other ideas started to make more sense, and now I think there is a clear direction for where to take this.

Here is some background:

  • On Linux it's possible to create fully static executables because the stable syscall ABI is at the interrupt level.
  • On Windows all executables dynamically link against kernel32.dll which is the stable syscall ABI.
  • On MacOS all executables dynamically link against libSystem which is the stable syscall ABI.
  • On FreeBSD all executables dynamically link against libc which is the stable syscall ABI.

So for an executable, there's not really any concept of "static" except on Linux. But even there, there's an easy way to determine if an executable should be static: it should be static if and only if all of its libraries are static, and this is only ever true for Linux and Freestanding.

I think we should remove the --static flag for executables. It really only applies to libraries.

@andrewrk
Copy link
Member

Mind if I hijack this issue and turn it into an accepted proposal?

@andrewrk andrewrk changed the title can't build static executables make --static only apply to libraries. link exes with static flag when all libraries are static. Dec 13, 2018
@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. accepted This proposal is planned. labels Dec 13, 2018
@andrewrk
Copy link
Member

--static is no longer an option that Zig offers. Instead, everything is as static as possible, and if you want something dynamic instead, use -dynamic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

2 participants