Should I provide custom code to handle stdlib.h when translating c in zig?

From zig guide, having errors with others .h files different than stddef.h

Why stdlib.h not found according to the path : zig\0.14.0\lib\libc\include\any-windows-any where all the .h files exist and are in the same directory with stddef.h?

main.c:2:10: error: 'stdlib.h' file not found
#include <stdlib.h>

Hello @jlouis
Welcome to ziggit :slight_smile:

Zig standard library does not need the C runtime library to operate. To include the C standard runtime simply link to it.
When invoking zig run or zig test from the command line, you can use the flag -lc.
When building with zig build, include either .link_libc = true in your module options or call exe.linkLibC();.

2 Likes

How do you build the C code, on which platform and does it involve cross-compiling?

E.g. this ‘just works’ here on macOS:

hello.c:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Hello World!\n");
}
zig cc hello.c -o hello

…the fact that (in your case) the compiler finds stddef.h but not stdlib.h is weird though.

You can inspect what the compiler is doing under the hood by compiling with -v (but better clear your global Zig cache before, otherwise zig cc will only invoke the linker - which is surprising and unfortunate IMHO):

zig cc hello.c -v -o hello

…on my Mac this prints (among others):

#include "..." search starts here:
#include <...> search starts here:
 /Users/floh/.zvm/master/lib/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/usr/include
 /opt/homebrew/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.4.sdk/System/Library/Frameworks (framework directory)
End of search list.

PS: in the build system, you’ll have to use .link_libc for the search paths to be set up though (the stddef.h vs stdlib.h thing is still weird though).

add -lc =)

I believe this is because stddef.h is part of freestanding C.

This is probably because macOS requires libc for now.

1 Like

Tbh, the -lc requirement is kinda weird in zig cc mode, since regular Clang doesn’t require explictly linking with libc either (at least I never encountered that problem, e.g. clang hello.c -o hello always worked on the platforms I tried.

 $ find ~/../usr/lib -name "crt*.o"
 /data/data/com.termux/files/home/../usr/lib/crtbegin_dynamic.o
 /data/data/com.termux/files/home/../usr/lib/crtbegin_so.o
 /data/data/com.termux/files/home/../usr/lib/crtbegin_static.o
 /data/data/com.termux/files/home/../usr/lib/crtend_android.o 
 /data/data/com.termux/files/home/../usr/lib/crtend_so.o
 /data/data/com.termux/files/home/../usr/lib/crt_pad_segment.o
        
$ uname -a                                 
Linux localhost 5.4.254-android12-9-
g619997ff2210 #1 SMP PREEMPT Fri Feb 7 14:59:01 CST 2025 aarch64 Android

$ zig cc hello.c
error: unable to create compilation: LibCRuntimeNotFound
                                               
$zig cc hello.c -Dtarget=aarch64-linux-musl -lc
error: unable to create compilation: LibCRuntimeNotFound

Regards!

By processing this cmd :

zig build item.zig […params]

or have to migrate 8000 lines of .c to .zig from scratch?