Say I have the following structure:
.\
|_ foo.c
|_ inc\
|_ foo_helper.c
|_ foo_helper.h
I want to obtain foo.ko. foo.c should #include <foo_helper.h>
. Also, a debug message with printk
is placed in the foo.c init function, just to see if the function was loaded.
My Kbuild file is:
obj-m := foo.o
foo-y := inc/foo_helper.o
ccflags-y := -I$(src)/inc/
Compiles fine. insmod
returns 0
; lsmod
lists the module as loaded, but the debug message is not printed in kern.log.
A minimal example:
- foo.c:
#undef __KERNEL__ #define __KERNEL__ #undef MODULE #define MODULE #include <linux/module.h> // included for all kernel modules #include <linux/kernel.h> // included for KERN_INFO #include <foo_helper.h> static int __init foo_init(void) { printk(KERN_INFO "Foo inserted successfully.\n"); foo_help_me(); return 0; } static void __exit foo_exit(void){} module_init(foo_init); module_exit(foo_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Foo"); MODULE_DESCRIPTION("Bar");
- inc/foo_helper.h:
#ifndef __FOO_CUSTOM_HELPER #define __FOO_CUSTOM_HELPER #include <linux/kernel.h> void foo_help_me(void); #endif /* __FOO_CUSTOM_HELPER */
- inc/foo_helper.c:
#include "foo_helper.h" void foo_help_me(void) { printk(KERN_INFO "We're inside the helper, seems to be working.\n"); }
- Kbuild:
obj-m := foo.o foo-y := inc/foo_helper.o ccflags-y := -I$(src)/inc
- Makefile:
ifneq ($(KERNELRELEASE),) include Kbuild else KDIR := /lib/modules/`uname -r`/build default: $(MAKE) -C $(KDIR) M=$$PWD modules endif