const std = @import(“std”);
const c = @cImport({
@cInclude("stdio.h");
@cInclude("stdlib.h");
@cInclude("string.h");
});
pub fn demonstrateBasicC() void {
// Call C printf directly
_ = c.printf("Hello from C printf: %d\n", 42);
// Use C memory allocation
const buffer = c.malloc(100);
if (buffer != null) {
_ = c.printf("Allocated %zu bytes\n", 100);
c.free(buffer);
}
}
pub fn main() void {
demonstrateBasicC();
}
$zig run foo.zig
foo.zig:2:11: error: C import failed
const c = @cImport({
^~~~~~~~
foo.zig:2:11: note: libc headers not available; compilation does not link against libc
referenced by:
demonstrateBasicC: foo.zig:10:9
main: foo.zig:21:22
5 reference(s) hidden; use ‘-freference-trace=7’ to see all references /data/data/com.termux/files/home/.cache/zig/o/91e76d6d833a96cd1beca2420489ff01/cimport.h:1:10: error: ‘stdio.h’ file not found
#include <stdio.h>
$cat bar.nim
proc printf(format: cstring): cint {.importc: "printf", varargs, header: "<stdio.h>".}
proc malloc(size: csize_t): pointer {.importc: "malloc", header: "<stdlib.h>".}
proc free(aptr: pointer) {.importc: "free", header: "<stdlib.h>".}
proc strlen(str: cstring): csize_t {.importc: "strlen", header: "<string.h>".}
\# Using Nim’s standard library wrapper import std/strutils
proc demonstrateBasicC() =
\# Call C printf directly
discard printf(“Hello from C printf: %d\\n”, 42)
\# Use C memory allocation
let buffer = malloc(100)
if buffer != nil:
discard printf("Allocated %zu bytes\n", 100)
free(buffer)
demonstrateBasicC()
foo.zig:2:11: error: C import failed
const c = @cImport({ ^\~\~\~\~\~\~\~ referenced by:
demonstrateBasicC: foo.zig:10:9 main: foo.zig:21:22
4 reference(s) hidden; use ‘-freference-trace=6’ to see all references
/data/data/com.termux/files/usr/bin/../../usr/include/linux/types.h:9:10:
error: ‘asm/types.h’ file not found
#include <asm/types.h>
…I would expect that Zig uses its embedded C toolchain headers.
Can you check if you have a global CFLAGS environment variable set? (e.g. echo $CFLAGS), while at it also check for a CXXFLAGS env variable. This is a surprisingly common problem for people running into similar header path confusion problems on Emscripten
PS: On my Linux laptop I need to build via zig run -lc bla.zig, but otherwise it also works as expected. My guess is still on a CFLAGS env variable adding header search paths to the C compile step.
$ zig libc
# The directory that contains `stdlib.h`.
# On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null`
include_dir=/data/data/com.termux/files/usr/bin/../../usr/include
# The system-specific include directory. May be the same as `include_dir`.
# On Windows it's the directory that includes `vcruntime.h`.
# On POSIX it's the directory that includes `sys/errno.h`.
sys_include_dir=/data/data/com.termux/files/usr/bin/../../usr/include
# The directory that contains `crt1.o` or `crt2.o`.
# On POSIX, can be found with `cc -print-file-name=crt1.o`.
# Not needed when targeting MacOS.
crt_dir=/data/data/com.termux/files/usr/bin/../../usr/lib
# The directory that contains `vcruntime.lib`.
# Only needed when targeting MSVC on Windows.
msvc_lib_dir=
# The directory that contains `kernel32.lib`.
# Only needed when targeting MSVC on Windows.
kernel32_lib_dir=
# The directory that contains `crtbeginS.o` and `crtendS.o`
# Only needed when targeting Haiku.
gcc_dir=
error: ld.lld: cannot open /data/data/com.termux/files/usr/bin/../../usr/lib/aarch64-linux-android/crtbegin_static.o: No such file or directory
error: ld.lld: unable to find library -lm
error: ld.lld: unable to find library -lc
error: ld.lld: unable to find library -ldl
error: ld.lld: cannot open /data/data/com.termux/files/usr/bin/../../usr/lib/aarch64-linux-android/crtend_android.o: No such file or directory
If you are trying to use android NDK then crt_dir must be <NDK_ROOT>/usr/lib/aarch64-linux-android/<NDK_LEVEL> and include_dir must be <NDK_ROOT>/usr/include
e.g. for API level 26 (Oreo/Android 8.0):