Skip to content
Advertisement

Ubuntu compiling kernel module first time

I’m trying to compile a simple kernel module for the first time:

#include <linux/module.h>    
#include <linux/kernel.h>

int init_nodule(void)
{
        printk(KERN_INFO "Hello world1.n");
        return 0;
}
void cleanup_module(void)
{
        printk(KERN_INFO "Goodbyen");
}

I’ve used obj-m += hello-1.o (that’s the name of the module) but i’m getting an error:

obj−m: command not found

Why is this happening? I tried looking online for a solution, but nothing I found helped..

EDIT: After modifying based on @Mathieu answer , I get the following error :

> make -C /lib/modules/4.18.0-15-generic/build M=/home/galco modules
make[1]: Entering directory '/usr/src/linux-headers-4.18.0-15-generic'
Makefile:970: "Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel"
scripts/Makefile.build:45: /home/galco/Makefile: No such file or directory
make[2]: *** No rule to make target '/home/galco/Makefile'.  Stop.
Makefile:1534: recipe for target '_module_/home/galco' failed
make[1]: *** [_module_/home/galco] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.18.0-15-generic'
makefile:4: recipe for target 'all' failed
make: *** [all] Error 2

Advertisement

Answer

The line obj-m += hello-1.o must be put in a file named Makefile

So it will looks like:

obj-m += hello-1.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

To launch the build process, just execute make from your command line.

More resource: https://qnaplus.com/how-to-compile-linux-kernel-module/

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement