'kernel Module on Raspberry pi 4

I am new to kernel Module and trying to build a kernel Module on Rpi, for simple Hello World : I am running this using VScode by connecting my RPI through SSH with VScode!

Following is my code:

#include <linux/module.h>               // Needed by all modules
#include <linux/kernel.h>               // Needed for KERN_ALERT
#include <linux/init.h>                 // Needed for the macros

MODULE_LICENSE("GPL");

static int
hello_2_init(void)
{
    printk(KERN_ALERT "Hello, world 2\n");
    return 0;
}

static void
hello_2_exit(void)
{
    printk(KERN_ALERT "Goodbye, world 2\n");
}

module_init(hello_2_init);
module_exit(hello_2_exit);

Following is my make file:

obj-m += HelloModule.o
CROSS_COMPILE = arm-linux-gnueabihf-
ARCH=arm

KDIR=/lib/modules/$(shell uname -r)/build

all:
    make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KDIR) M=$(PWD) modules

clean:
    make -C $(KDIR) M=$(PWD) clean

I have navigated to the respective folder and after trying make all following is the error that I am getting:

make[1]: Entering directory '/usr/src/linux-headers-5.10.103-v7l+'
make[2]: *** No rule to make target '/home/pi/Documents/testfolder2/HelloModule.o', needed by '/home/pi/Documents/testfolder2/HelloModule.mod'.

Can somebody please help me I am just stuck on this for the past two days!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source