building simple kernel module mkmakefile: No such file or directory - c

I was compiling and building simple kernel module on linux
following is my Makefile
obj-m := hello-1.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.* *.symvers *.order
when I run make command against the makefile I get error that mkmakefile missing
make[1]: Entering directory '/usr/src/linux-headers-5.9.0-kali2-amd64'
sh: 0: cannot open /usr/src/linux-headers-5.9.0-kali2-common/scripts/mkmakefile: No such file
make[3]: *** [/usr/src/linux-headers-5.9.0-kali2-common/Makefile:552: outputmakefile] Error 2
/usr/src/linux-headers-5.9.0-kali2-common/Makefile:691: include/config/auto.conf.cmd: No such file or directory
make[2]: *** [/usr/src/linux-headers-5.9.0-kali2-common/Makefile:710: include/config/auto.conf.cmd] Error 2
make[1]: *** [/usr/src/linux-headers-5.9.0-kali2-common/Makefile:185: __sub-make] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.9.0-kali2-amd64'
enter code here
make: *** [Makefile:6: default] Error 2
the from this page https://forums.kali.org/showthread.php?46712-Kali-2020-1-VMWARE-installing-Alfa-AWUS036ACM
i changed my make file following line
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
Now it's giving different error that auto.conf is missing
make -C /lib/modules/5.9.0-kali2-amd64/build M=/home/fawad/Desktop/infoFolder/Linux/KernelDevelopment/hell modules
make[1]: Entering directory '/usr/src/linux-headers-5.9.0-kali2-amd64'
ERROR: Kernel configuration is invalid.
include/generated/autoconf.h or include/config/auto.conf are missing.
Run 'make oldconfig && make prepare' on kernel src to fix it.
make[2]: *** [/usr/src/linux-headers-5.9.0-kali2-common/Makefile:718: include/config/auto.conf] Error 1
make[1]: *** [/usr/src/linux-headers-5.9.0-kali2-common/Makefile:185: __sub-make] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.9.0-kali2-amd64'
make: *** [Makefile:6: default] Error 2
I found on internet I have to run make prepare command against Makefile in /usr/src/linux-headers-5.9.0-kali2-common/Makefilebut now its again giving me an first error
sh: 0: cannot open /usr/src/linux-headers-5.9.0-kali4-common/scripts/mkmakefile: No such file
/usr/src/linux-headers-5.9.0-kali4-common/Makefile:691: include/config/auto.conf.cmd: No such file or directory
make[1]: *** [/usr/src/linux-headers-5.9.0-kali4-common/Makefile:710: include/config/auto.conf.cmd] Error 2
make: *** [/usr/src/linux-headers-5.9.0-kali4-common/Makefile:185: __sub-make] Error 2
I like to know how to configure make and linux so I can build external modules? like this one
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);

Related

Makefile fails to build kernel module "recipe commences before first target"

I tried to build example 2.1 from https://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf.
I tried googling for answers and all related threads came down on the Makefile being copied over wrong. So i tried retyping the makefile and i also tried other peoples Makefile. But i keep getting the same errors.
lkm.c:
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
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
This is the error i get:
philip#ubuntu:~/test$ make
make -C /lib/modules/5.0.0-20-generic/build M=/home/philip/test modules
make[1]: Entering directory '/usr/src/linux-headers-5.0.0-20-generic'
make[2]: *** No rule to make target '/home/philip/test/lkm.c', needed by '/home/philip/test/lkm.o'. Stop.
make[1]: *** [Makefile:1605: _module_/home/philip/test] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.0.0-20-generic'
make: *** [Makefile:4: all] Error 2
Solved! The file in my directory was called lkm instead of lkm.c. Thanks for the replies :)

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 "Goodbye\n");
}
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
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/

Error generating .ko file - ubuntu make fails with "recipe for target 'all' failed"

I want to load module and driver.
At first there were errors like:
CONFIG_UNWINDER_ORC = y, please install libelf-dev, libelf-devel or elfutils-libelf-devel
So I did the actions as stated here:
https://askubuntu.com/questions/1070066/how-to-install-libelf-devel-or-elfutils-libelf-devel-in-ubuntu-18-04-hp-laptop-a
But still, I can not create a ko file.
This is the Error:
make -C /lib/modules/4.15.0-48-generic/build M=/home/ex6 modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-48-generic'
make[1]: Warning: File '/home/ex6/.cache.mk' has modification time 0.56 s in the future
make[2]: Warning: File '/home/ex6/.cache.mk' has modification time 0.52 s in the future
CC [M] /home/ex6/chardev.o
/home/ex6/chardev.o: Operation not permitted
/home/ex6/chardev.o: failed
scripts/Makefile.build:337: recipe for target '/home/ex6/chardev.o' failed
make[2]: *** [/home/ex6/chardev.o] Error 1
Makefile:1552: recipe for target '_module_/home/ex6' failed
make[1]: *** [_module_/home/ex6] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-48-generic'
Makefile:6: recipe for target 'all' failed
make: *** [all] Error 2
This is the Makefile:
obj-m := chardev.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean

compile kernel module error

I wish to run this kernel module
code file hello.c
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void) {
printk(KERN_INFO "Hello world!\n");
return 0;
}
void cleanup_module(void) {
printk(KERN_INFO "Goodbye world!\n");
}
i ran the makefile below
obj-m += hello.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
but I am getting the following errors
make -C /lib/modules/4.4.0-45-generic/build M=/home/fyousry/Desktop/Untitled Folder 4 modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-45-generic'
arch/x86/Makefile:148: CONFIG_X86_X32 enabled but no binutils support
Makefile:676: Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-strong not supported by compiler
make[1]: *** No rule to make target 'Folder'. Stop.
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-45-generic'
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 2
the directory which Contain this code has space
when i removed space it is work (UntitledFolder4 instead of Untitled Folder 4)
You should use double quotes when you give a path to a command, especially if the path is contain a sub-command or a variable.
Example:
make -C "/lib/modules/$(shell uname -r)/build" "M=$(PWD)" modules
You can also escape the space if you want to keep it spaced :
make -C /lib/modules/4.4.0-45-generic/build M=/home/fyousry/Desktop/Untitled\ Folder\ 4 modules

makefile for a libusb program

I have made a C program using libusb and I am using following command to compile it:
gcc -o usbtest.o usbtest.c -lusb-1.0
The program is working fine. Next, I added the code of "usbtest.c" to kernel module (usbmod.c) and I am stuck with the make file. I am not sure what command I should pass in the "all" section. Here is what I have made:
obj-m := usbmod.o
KERNEL_DIR = /lib/modules/$(shell uname -r)/build
PWD = $(shell pwd)
all:
$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.* *.symvers *.order *-
After running make, I am getting the following error:
anubhav#anubhav-Inspiron-3421:~/Desktop/usb$ make
make -C /lib/modules/3.13.0-46-generic/build SUBDIRS=/home/anubhav/Desktop/usb modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-46-generic'
CC [M] /home/anubhav/Desktop/usb/usbmod.o
/home/anubhav/Desktop/usb/usbmod.c:3:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.
make[2]: *** [/home/anubhav/Desktop/usb/usbmod.o] Error 1
make[1]: *** [_module_/home/anubhav/Desktop/usb] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-46-generic'
make: *** [all] Error 2
Not sure if need to bring any header file to my working directory or what. Kindly provide suggestions.
Okay, so this link clarifies my doubts to some extent:
[][1]error: stdio.h: No such file or directory error during make
It says "stdio.h" and all do not exist in kernel space and therefore such errors arise. Besides, obviously my module does not contain any printf so I suppose I don't need "stdio.h".
But it does use libusb extensively. So, is there a way to really create this module.

Resources