zig icon indicating copy to clipboard operation
zig copied to clipboard

Improve alternative libc support

Open RossComputerGuy opened this issue 11 months ago • 6 comments

Right now, I'm looking to develop my own Linux distro using Zig and I have to supply a libc. A libc implementation which I am interested in using is LLVM's libc, I am also interested in using a pure Zig libc but it is harder to detect and supply what the libc. Currently, these are the things I am looking for Zig being able to do:

  • Builder API to supply libc library + headers
  • Programmatic function / definition detection

RossComputerGuy avatar Mar 18 '24 04:03 RossComputerGuy

What about --libc <file> and zig libc?

ikskuh avatar Mar 22 '24 18:03 ikskuh

The problem I have is:

  1. Scanning which functions are available
  2. std.Target detection

RossComputerGuy avatar Mar 22 '24 18:03 RossComputerGuy

  1. weak export all of them
  2. builtin.target

re 1 if its just a matter of wanting to know which ones to implement first, remove -lc and see which symbols the linker starts complaining about being missing

nektro avatar Mar 22 '24 19:03 nektro

What about --libc <file> and zig libc?

This reminds me a reference from Andrew.

  • https://github.com/ziglang/zig/issues/514#issuecomment-467172534

When it comes to C code, it's common to use -nostdlib and -nostdinc, but in the zig build interface it gets complicated, leaving only the absence of .linkLibC as an alternative solution.


I recently tried to port picolibc to zig build. However, so far I have only encountered a link problem.

As is currently the case with emscripten, it would be interesting to add initial support for the rtos and microlibc interfaces, since cross-compilation is also mentioned in microcontrollers.

kassane avatar Mar 23 '24 02:03 kassane

@kassane Was your linker issue related to libc, libm, libutil, etc not being found? I ran into a similar issue when I tried adding my own libc to Zig. The only thing I did was add it into the abi enum and any switches which didn't have an else.

RossComputerGuy avatar Mar 23 '24 21:03 RossComputerGuy

Hi @RossComputerGuy ,

Sorry for the delay in responding.

Yes, I've been having problems with linking, specifically link scripting with lld instead of gnu-ld. Picolibc has a modification to generate a link script for lld (meson), but I need to do more testing on it.

The most critical part of these tests is the toolchain, as compatibility is never 100% (maybe 99.9%) and this where more attention to detail is required, especially for a solo developer.

kassane avatar Mar 24 '24 23:03 kassane

Yes, I've been having problems with linking, specifically link scripting with lld instead of gnu-ld. Picolibc has a modification to generate a link script for lld (meson), but I need to do more testing on it.

Oh, gotcha. Yeah when I tried adding my own libc into my Zig fork, I ran into this linker issue:

error: ld.lld: unable to find library -lm
error: ld.lld: unable to find library -lpthread
error: ld.lld: unable to find library -lc
error: ld.lld: unable to find library -ldl
error: ld.lld: unable to find library -lrt
error: ld.lld: unable to find library -lutil

I got these errors despite targeting glibc, seems like it isn't straight forward to add new libc support into Zig. I'm hoping with this issue that we can get a simpler way to specify custom or unsupported libc without patching Zig.

RossComputerGuy avatar Mar 25 '24 05:03 RossComputerGuy