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>
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();.
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).
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.