-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
zig cc: detect -static and treat -l options as static library names #4986
Comments
Also, on an unrelated note - is it possible to do "--strip" (like in |
For the other problem- I looked through the code a bit and I believe the problem is If you are looking for a workaround in the meantime, it is to pass the .a files as positional arguments instead of |
@andrewrk I'm not sure if I'm doing something wrong but it doesn't seem like strip is enabled by default, with the same C file: #include <stdio.h>
int main()
{
printf("Hello World!\n");
} $ zig cc -target x86_64-linux-musl -static -O3 hello.c
$ file file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
$ du -sh hello
12K hello
$ strip hello
$ du -sh hello
8.0K hello
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped |
Is that different than |
Not sure if this is the same issue, but Edit: Also, Zig doesn't even seem to know the options:
|
Facing the same issue as @makeworld-the-better-one, also doesn't work when using lld as a linker. |
As @andrewrk suggested, there is a workaround by using object files ( const exe = b.addExecutable("exe", null);
exe.addObjectFile("/path/to/a-file"); |
Could someone please provide a workaround for using I tried following command, but still get
This command builds without a problem:
|
UPDATE: I misunderstood this issue:
Just building the hello world example without any
#include <stdio.h>
int main()
{
printf("Hello World!\n");
} # zig 0.11.0 (Aug 2023) via Fedora 40 Docker image:
$ zig cc -static -o hello hello.c
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, with debug_info, not stripped
$ ldd hello
linux-vdso.so.1 (0x00007fff22ad0000)
libc.so.6 => /lib64/libc.so.6 (0x00007f59ddc91000)
/lib64/ld-linux-x86-64.so.2 (0x00007f59dde83000)
# Explicit target, same output except for `GNU/Linux 2.0.0`:
$ zig cc -static -target x86_64-linux-gnu -o hello hello.c
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.0.0, with debug_info, not stripped
# Musl target is static, unlike glibc/gnu:
$ zig cc -static -target x86_64-linux-musl -o hello hello.c
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped
$ ldd hello
not a dynamic executable I imagine that's the actual issue here? UPDATE: Nevermind, I understand the advice/discussion now 😅 To successfully static link the hello world on a
So you need to also reference On fedora these can be found: # Find a package that provides this file, in this case it's `gcc`:
$ dnf provides *gcc_eh.a
gcc-14.0.1-0.7.fc40.x86_64 : Various compilers (C, C++, Objective-C, ...)
Repo : @System
Matched from:
Other : *gcc_eh.a
# Query the package for the actual install path:
$ dnf repoquery -l gcc | grep gcc_eh.a
/usr/lib/gcc/x86_64-redhat-linux/14/32/libgcc_eh.a
/usr/lib/gcc/x86_64-redhat-linux/14/libgcc_eh.a
# Same process for `libc.a`:
$ dnf provides *libc.a
glibc-static-2.39-2.fc40.x86_64 : C library static libraries for -static linking.
Repo : @System
Matched from:
Other : *libc.a
$ dnf repoquery -l glibc-static | grep libc.a
/usr/lib/libc.a
/usr/lib64/libc.a So until this issue is fixed, it'll vary based on build host: # Split to multi-line for readability:
$ zig cc -static -target x86_64-linux-gnu -o hello \
hello.c \
/usr/lib64/libc.a \
/usr/lib/gcc/x86_64-redhat-linux/14/libgcc_eh.a
$ ldd hello
statically linked
# Despite that, seems to still be dynamically linked?
$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.0.0, with debug_info, not stripped
$ nm -an hello | grep dl_open
0000000000290e60 t dl_open_worker
0000000000291190 t _dl_open
00000000002914c0 t dl_open_worker_begin Perhaps it needs something else for
Related issues: |
So I'm not sure if I'm doing something incorrectly, but I can't really figure out, so I came with this reproducible example.
Consider we have a C file:
We want to build it for
arm-linux-musleabi
as a static binary and link tolibressl
, so we do this:After that we build our C file:
ldd
tells that it's in fact not a dynamic binary:But if we try to run
file
on it:Or even if we do
strings hello
,/lib/ld-musl-arm.so.1
will be there too.And we won't be able to run it on any ARM-based machine which doesn't have that file:
(Maybe I just used some compiler options incorrectly, but I'm not really experienced with C compiler arguments)
The text was updated successfully, but these errors were encountered: