Linux Kernel Modules : i have problem in Creating Kernel Modules when i compile the code of it it display an error (#include <linux/init.)
by jakliza from LinuxQuestions.org on (#6QGDE)
--------------------------------------
Code(in ubuntu)
--------------------------------------
nano LKM.c:
---------
#include <linux/module.h> // Core header for all kernel modules
// For KERN_INFO
#include <linux/init.h> // For __init and __exit macros
// Module initialization function
static int __init hello_init(void) {
printk(KERN_INFO "Hello, World!\n");
return 0; // Return 0 indicates successful initialization
}
// Module cleanup function
static void __exit hello_exit(void) {
printk(KERN_INFO "Goodbye, World!\n");
}
// Register the initialization and cleanup functions
module_init(hello_init);
module_exit(hello_exit);
----------------------------------
The makefile:
-----------------------------------
nano makefile
obj-m += LKM.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
-----------------------------------
finally write make in the console
when code and makefile are
----------------------------------
make
--------------------------------
The outputs it display an error?
--------------------------------
make -C /lib/modules/6.8.0-31-generic/build M=/home/ubuntu/os_learn modules
make[1]: Entering directory '/usr/src/linux-headers-6.8.0-31-generic'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0
You are using: gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0
scripts/Makefile.build:41: /home/ubuntu/os_learn/Makefile: No such file or directory
make[3]: *** No rule to make target '/home/ubuntu/os_learn/Makefile'. Stop.
make[2]: *** [/usr/src/linux-headers-6.8.0-31-generic/Makefile:1926: /home/ubuntu/os_learn] Error 2
make[1]: *** [Makefile:240: __sub-make] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-6.8.0-31-generic'
make: *** [makefile:3: all] Error 2
Code(in ubuntu)
--------------------------------------
nano LKM.c:
---------
#include <linux/module.h> // Core header for all kernel modules
// For KERN_INFO
#include <linux/init.h> // For __init and __exit macros
// Module initialization function
static int __init hello_init(void) {
printk(KERN_INFO "Hello, World!\n");
return 0; // Return 0 indicates successful initialization
}
// Module cleanup function
static void __exit hello_exit(void) {
printk(KERN_INFO "Goodbye, World!\n");
}
// Register the initialization and cleanup functions
module_init(hello_init);
module_exit(hello_exit);
----------------------------------
The makefile:
-----------------------------------
nano makefile
obj-m += LKM.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
-----------------------------------
finally write make in the console
when code and makefile are
----------------------------------
make
--------------------------------
The outputs it display an error?
--------------------------------
make -C /lib/modules/6.8.0-31-generic/build M=/home/ubuntu/os_learn modules
make[1]: Entering directory '/usr/src/linux-headers-6.8.0-31-generic'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0
You are using: gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0
scripts/Makefile.build:41: /home/ubuntu/os_learn/Makefile: No such file or directory
make[3]: *** No rule to make target '/home/ubuntu/os_learn/Makefile'. Stop.
make[2]: *** [/usr/src/linux-headers-6.8.0-31-generic/Makefile:1926: /home/ubuntu/os_learn] Error 2
make[1]: *** [Makefile:240: __sub-make] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-6.8.0-31-generic'
make: *** [makefile:3: all] Error 2