I have use this to compile GitHub - Southporter/LKModule-zig: Zig Kernel Module to zigmodule.ko
I want to hack this project to like a demo like a hello world function in kernel kernel module, like this project: GitHub - carloscdias/hello-world-linux-module: A simple Linux kernel module to be used as example
the zigmodule.zig file I have to change to
const std = @import("std");
const builtin = @import("builtin");
const c = @cImport({
@cInclude("linux/kernel.h");
});
// Switch based on tag, as using target / mode is trickier
// without full std lib access.
// const print = (if (builtin.os.tag == .freestanding) @import("kernel.zig") else @import("debug.zig"))
// .print;
extern fn printk(fmt: [*:0]const u8, ...) void;
export fn init_hellokernel() c_int {
_ = c.printk("{Hello kernel!");
return 0;
}
export fn exit_hellokernel() void {
_ = c.printk("Goodbye kernel!");
}
test "test1" {
_ = c.printk("\n{s}\n", .{"Hello stdout \"kernel\"!"});
}
but I got this error
I have used obj.addSystemIncludePath(b.path("/lib/modules/5.15.0-105-generic/build/include")); to include the kernel header, looks like is not right
Can someone to help me to solve this problems? thanks a lot
If you look thoroughly at the error message, you’ll see that the problem is
...note: libc headers not available...
In order to use c includes, you need to link the c standard libary:
exe.linkLibC(); // build.zig
-lc // command line option
3 Likes
I have linked what I have, but also have this problem.
Is there something wrong? helps , thanks a lot
I think we need to add the system include path to this. Currently it only adds the local include directory. I’m not sure how to get an absolute path for obj.addSystemIncludePath(...) to get the /usr/include path added.
Running the compile command directly with -isystem /usr/include gets it past the cimport file not found issue.
Just figured it out. Add the following to build.zig
// Change the path to where ever you have the headers installed
obj.addSystemIncludePath(.{ .cwd_relative = "/usr/include" });
follow your way, but got another error, looks like that build tools generate a source file struct which does not contain a printk member
Sze
August 8, 2025, 12:09pm
6
@rebornwwp please stop over-using screenshots of text, you can use:
```
text / code goes here
```
There is also a “preformatted text” (ctrl+e) button.
3 Likes
What headers path are you using? I ran into that error when I used /usr/include. I had to install kernel-devel (on fedora) and add the paths for those headers.
It looks like you may have to use this path instead if that is where you have the development headers at:
obj.addSystemIncludePath(.{ .cwd_relative = "/lib/modules/5.15.0-105-generic/build/include" });
I have use your method to solve this kind of problem
obj.linkLibC();
obj.addSystemIncludePath(b.path("include"));
obj.addSystemIncludePath(.{ .cwd_relative = "/lib/modules/6.15.9-201.fc42.x86_64/build/include" });
obj.addSystemIncludePath(.{ .cwd_relative = "/usr/include" });
obj.addSystemIncludePath(.{ .cwd_relative = "/usr/src/kernels/6.15.9-201.fc42.x86_64/tools/include" });
obj.addSystemIncludePath(.{ .cwd_relative = "/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/generated/uapi" });
obj.addSystemIncludePath(.{ .cwd_relative = "/usr/src/kernels/6.15.9-201.fc42.x86_64/include/uapi" });
obj.addSystemIncludePath(.{ .cwd_relative = "/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86//include" });
but got this error
âžś LKModule-zig git:(main) âś— make
zig build \
&& touch /home/reborn/workspace/x/LKModule-zig/zig-out/obj/.zigmodule.o.cmd
install
└─ install zigmodule
└─ zig build-obj zigmodule ReleaseSmall native-freestanding 21 errors
src/zigmodule.zig:24:11: error: C import failed
const c = @cImport({
^~~~~~~~
referenced by:
exit_hellokernel: src/zigmodule.zig:42:9
exit_hellokernel: src/zigmodule.zig:41:1
4 reference(s) hidden; use '-freference-trace=6' to see all references
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:55:23: error: expected string literal in 'asm'
asm_inline volatile(LOCK_PREFIX "orb %b1,%0"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:60:23: error: expected string literal in 'asm'
asm_inline volatile(LOCK_PREFIX __ASM_SIZE(bts) " %1,%0"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:68:15: error: expected string literal in 'asm'
asm volatile(__ASM_SIZE(bts) " %1,%0" : : ADDR, "Ir" (nr) : "memory");
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:75:23: error: expected string literal in 'asm'
asm_inline volatile(LOCK_PREFIX "andb %b1,%0"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:79:23: error: expected string literal in 'asm'
asm_inline volatile(LOCK_PREFIX __ASM_SIZE(btr) " %1,%0"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:94:15: error: expected string literal in 'asm'
asm volatile(__ASM_SIZE(btr) " %1,%0" : : ADDR, "Ir" (nr) : "memory");
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:101:22: error: expected string literal in 'asm'
asm_inline volatile(LOCK_PREFIX "xorb %2,%1"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:118:15: error: expected string literal in 'asm'
asm volatile(__ASM_SIZE(btc) " %1,%0" : : ADDR, "Ir" (nr) : "memory");
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:125:23: error: expected string literal in 'asm'
asm_inline volatile(LOCK_PREFIX "xorb %b1,%0"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:129:23: error: expected string literal in 'asm'
asm_inline volatile(LOCK_PREFIX __ASM_SIZE(btc) " %1,%0"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:137:26: error: expected string literal in 'asm'
return GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, c, "Ir", nr);
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:151:6: error: expected string literal in 'asm'
asm(__ASM_SIZE(bts) " %2,%1"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:161:26: error: expected string literal in 'asm'
return GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btr), *addr, c, "Ir", nr);
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:177:15: error: expected string literal in 'asm'
asm volatile(__ASM_SIZE(btr) " %2,%1"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:189:15: error: expected string literal in 'asm'
asm volatile(__ASM_SIZE(btc) " %2,%1"
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:200:26: error: expected string literal in 'asm'
return GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(btc), *addr, c, "Ir", nr);
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:214:8: error: expected ')'
CC_SET(nz)
^
/usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/asm/bitops.h:227:15: error: expected string literal in 'asm'
asm volatile(__ASM_SIZE(bt) " %2,%1"
^
/lib/modules/6.15.9-201.fc42.x86_64/build/include/linux/bitops.h:76:1: error: use of undeclared identifier '___set_bit'
__check_bitop_pr(__set_bit);
^
error: too many errors emitted, stopping now
error: the following command failed with 21 compilation errors:
/home/reborn/bin/zig/zig build-obj -fstrip -OReleaseSmall -mcmodel kernel -target native-freestanding -mcpu native -isystem /home/reborn/workspace/x/LKModule-zig/include -isystem /lib/modules/6.15.9-201.fc42.x86_64/build/include -isystem /usr/include -isystem /usr/src/kernels/6.15.9-201.fc42.x86_64/tools/include -isystem /usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include/generated/uapi -isystem /usr/src/kernels/6.15.9-201.fc42.x86_64/include/uapi -isystem /usr/src/kernels/6.15.9-201.fc42.x86_64/arch/x86/include -Mroot=/home/reborn/workspace/x/LKModule-zig/src/zigmodule.zig -lc --cache-dir /home/reborn/workspace/x/LKModule-zig/.zig-cache --global-cache-dir /home/reborn/.cache/zig --name zigmodule -fno-compiler-rt --zig-lib-dir /home/reborn/bin/zig/lib/ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install zigmodule transitive failure
└─ zig build-obj zigmodule ReleaseSmall native-freestanding 21 errors
error: the following build command failed with exit code 1:
/home/reborn/workspace/x/LKModule-zig/.zig-cache/o/03add279d7d93b2898042cb2e1964d40/build /home/reborn/bin/zig/zig /home/reborn/bin/zig/lib /home/reborn/workspace/x/LKModule-zig /home/reborn/workspace/x/LKModule-zig/.zig-cache /home/reborn/.cache/zig --seed 0x24bfb058 -Z2318f8e6d27ba643
make: *** [Makefile:28: zig.o] Error 1
help to solve this problem. thanks a lot
That is looking like it may be a translateC bug or limitation. These headers may be using some aspects of C that Zig cannot translate properly yet. And this is using inline assembly, which still has an open issue: inline assembly improvements · Issue #215 · ziglang/zig · GitHub
If you know which kernel functions you are going to use, you could create a file like the src/ffi.zig function where you define all of them as extern with the correct function signature, and use that instead. It will get you the same result, but you will have to manually translate the C to zig.
1 Like
Thanks very much! I will try this method, hope this can solve this problem
1 Like