# Zig, Libc and SysCalls

**URL:** <https://ziggit.dev/t/zig-libc-and-syscalls/5696>\
**Category:** Explain\
**Tags:** language, compiler, build-system\
**Created:** [August 20, 2024, 12:46pm UTC](https://ziggit.dev/t/zig-libc-and-syscalls/5696 "2024-08-20T12:46:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![finlaydotb](https://ziggit.dev/user_avatar/ziggit.dev/finlaydotb/32/3462_2.png) [@finlaydotb](https://ziggit.dev/u/finlaydotb)\
**Post date:** [August 20, 2024, 12:46pm UTC](https://ziggit.dev/t/zig-libc-and-syscalls/5696/1 "2024-08-20T12:46:27Z")

</div>

What is the relationship between Zig and C libraries (libc, glibc, musl-libc etc) and direct sys calls on different Oses?

Do zig always use a C library?

If so what determines the C library Zig uses?

Can I override the default C library Zig uses?

Can I force Zig to not use any C library and only go things via Sys calls?

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [August 20, 2024, 1:43pm UTC](https://ziggit.dev/t/zig-libc-and-syscalls/5696/2 "2024-08-20T13:43:47Z")

</div>

By default zig will not link the C standard library.  
All of the code inside the Zig standard library uses direct syscalls.

You can link the C standard library by passing `-lc` to the command or using `.linkLibC()` inside the build.zig.

The C library is determined by the target as far as I know. So if you target `x86_64-linux-gnu` it will use glibc. If you target `x86_64-linux-musl` it will use musl libc. You can change the target either from the command `-Dtarget=...` or inside of the build.zig file.

---

<div class="post-metadata">

**Author:** ![finlaydotb](https://ziggit.dev/user_avatar/ziggit.dev/finlaydotb/32/3462_2.png) [@finlaydotb](https://ziggit.dev/u/finlaydotb)\
**Post date:** [August 20, 2024, 2:09pm UTC](https://ziggit.dev/t/zig-libc-and-syscalls/5696/3 "2024-08-20T14:09:45Z")

</div>

> [@IntegratedQuantum](#):
>
> All of the code inside the Zig standard library uses direct syscalls.

I think I read somewhere that by default, on MacOs, Zig uses libc.

How would one confirm this?

---

<div class="post-metadata">

**Author:** ![permutationlock](https://ziggit.dev/user_avatar/ziggit.dev/permutationlock/32/9958_2.png) [@permutationlock](https://ziggit.dev/u/permutationlock)\
**Post date:** [August 20, 2024, 6:51pm UTC](https://ziggit.dev/t/zig-libc-and-syscalls/5696/4 "2024-08-20T18:51:46Z")

</div>

To look at target specific defaults you can use:

```shell
zig build-exe -target aarch64-macos --show-builtin > builtin.zig

```

Here we find:

```zig
pub const link_libc = true;

```

---

<div class="post-metadata">

**Author:** ![finlaydotb](https://ziggit.dev/user_avatar/ziggit.dev/finlaydotb/32/3462_2.png) [@finlaydotb](https://ziggit.dev/u/finlaydotb)\
**Post date:** [August 20, 2024, 6:58pm UTC](https://ziggit.dev/t/zig-libc-and-syscalls/5696/5 "2024-08-20T18:58:32Z")

</div>

Yes I confirmed `link_libc = true`

So I guess this confirms that by default on MacOs, direct syscalls are not used but libc is used instead.

---

<div class="post-metadata">

**Author:** ![permutationlock](https://ziggit.dev/user_avatar/ziggit.dev/permutationlock/32/9958_2.png) [@permutationlock](https://ziggit.dev/u/permutationlock)\
**Post date:** [August 20, 2024, 7:04pm UTC](https://ziggit.dev/t/zig-libc-and-syscalls/5696/6 "2024-08-20T19:04:04Z")

</div>

Outside of Linux, most operating systems don’t guarantee a stable system call ABI for making system calls directly through the assembly instruction. They intend for you to use the OS userspace libraries (e.g. libc or the various windows libraries) to make system calls.

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [August 21, 2024, 8:06pm UTC](https://ziggit.dev/t/zig-libc-and-syscalls/5696/7 "2024-08-21T20:06:20Z")

</div>

> [@IntegratedQuantum](#):
>
> You can link the C standard library by passing `-lc` to the command or using `.linkLibC()` inside the build.zig.

This is correct, but I encourage you to use the `build.zig` version, `.linkLibC()`. There are changes in the pipeline to separate out the LLVM and C-compiling parts of Zig, and that version is more likely to survive those changes intact.
