no such file or directory error. What is wrong? - c

I'm trying to build a kernel module. I'm on fedora 25
Here is my Makefile:
obj-m += simple.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 simple.c file:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Module\n");
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");
The error I'm getting after using the make command:
make -C /lib/modules/4.11.3-202.fc25.x86_64/build M=/home/nubian/Downloads/ch02/ch2 modules
make[1]: *** /lib/modules/4.11.3-202.fc25.x86_64/build: No such file or directory. Stop.
Makefile:7: recipe for target 'default' failed
make: *** [default] Error 2

You should always use $(MAKE) never make when you invoke a recursive make. However that's not your problem.
Your problem is that you don't have the module build environment for your currently-running kernel installed. If you install it, it will create the directories you need to build modules for your kernel.
See, for example, the yum install command on this page.

Related

Cannot use a Makefile to comple kernel files in Ubuntu Linux on WSL

I have some C code and a Makefile for that C code. The C program is called simple.c and the the makefile is just called Makefile.
Here are the contents of simple.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Kernel Module\n");
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void)
{
printk(KERN_INFO "Removing Kernel Module\n");
}
/* Macros for registering module entry and exit points. */
module_init(simple_init);
module_exit(simple_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");
And this is the content of the Makefile:
obj-m += simple.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
I have both of them in the same directory and I use the instruction make to compile it however it does not work and I get this error message:
make -C /lib/modules/5.15.79.1-microsoft-standard-WSL2/build M=/home/eddie modules make[1]: *** /lib/modules/5.15.79.1-microsoft-standard-WSL2/build: No such file or directory. Stop. make: *** [Makefile:3: all] Error 2
I have tried to install some things I have seen online for linux kernal but they do not seem to work.
I have tried the following:
sudo apt install linux-tools-virtual hwdata
sudo apt-get install linux-headers-$(uname -r)
It's important to note that these all do sucessfully install. When I use lsmod to list all current kernal modules nothing is listed.
Am I doing anything wrong? Missing a command to install something? I am using Ubuntu Linux subsystem for linux on windows 11

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 :)

Linux kernel module programming - Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: -fstack-protector not supported by compiler

I am on Linux Ubuntu 14.04. I want to start Linux Kernel Module Programming. I have hello.c (simple Hello World module) and Makefile. But, on "make" command, I get error.
I tried Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-strong not supported by compiler , but it did not work for me.
hello.c
/* hello.c − Illustrating the __init, __initdata and __exit macros. */
#include <linux/module.h>
/* Needed by all modules */
#include <linux/kernel.h>
/* Needed for KERN_INFO */
#include <linux/init.h>
/* Needed for the macros */
static int hello3_data __initdata = 3;
static int __init hello_3_init(void)
{
printk(KERN_INFO "Hello, world %d\n", hello3_data);
return 0;
}
static void __exit hello_3_exit(void)
{
printk(KERN_INFO "Goodbye, world 3\n");
}
module_init(hello_3_init);
module_exit(hello_3_exit);
Makefile
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
On "make" :-
k#k-Inspiron-3542:~/Kernel programs$ make
make -C /lib/modules/4.2.0-27-generic/build M=/home/k/Kernel programs modules
make[1]: Entering directory `/usr/src/linux-headers-4.2.0-27-generic'
arch/x86/Makefile:138: CONFIG_X86_X32 enabled but no binutils support
Makefile:662: Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: -fstack-protector not supported by compiler
make[1]: *** No rule to make target `programs'. Stop.
make[1]: Leaving directory `/usr/src/linux-headers-4.2.0-27-generic'
make: *** [all] Error 2
Currently, my ubuntu has 4.2 kernel. I even tried this on 3.x kernel, but there was this same error.
Please help me in this. Thanks. :)
I had searched a lot before asking this question, but no solution worked for me. I continued my search and finally, this solution worked for me. https://askubuntu.com/questions/367838/compiling-error-while-installing-realtek-rtl8111e-in-64-bit-13-10-config-x86-x
Strangely, there should be no space(s) in the directory name in which kernel modules are there. So, I removed the space and it worked.
Hope it will help someone in future. :)
Your file works OK here. With your Makefile, and with the default Makefile:
obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
The files hello.ko, hello.mod.c, hello.mod.o, hello.o, modules.order, Module.symvers are created. May be install this: sudo apt install g++ binutils-dev
If you don't have spaces in your compile directory and you're still getting this error, your kernel compilation may be failing because its directory belongs to root and you're running as an unprivileged user. Try sudo make

Error in running Makefile (in C) when creating a Linux Module

I have a code below and I need to compile it. However, I can not run the Makefile on my computer
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
int
simple_init(void)
{
printk("Loading module\n");
return 0;
}
void
simple_exit(void)
{
printk("Removing module\n");
}
module_init(simple_init);
module_exit(simple_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
Makefile:
obj-m := simple.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
The problem is that: When I type Make in the terminal the following error occurs:
make -C /lib/modules/4.4.0-75-generic/build
SUBDIRS=/home/caiquefortunato/Área de Trabalho/kernel modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-75-generic'
arch/x86/Makefile:148: CONFIG_X86_X32 enabled but no binutils support
Makefile:693: Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-
protector-strong not supported by compiler
make[1]: *** No rule to make target 'de'. Pare.
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-75-generic'
Makefile:19: recipe for target 'default' failed
make: *** [default] Error 2
What do I have to do to make it work? Do I need to install something?
Thank you very much in advance
There are a few causes of the CONFIG_X86_X32 enabled but no binutils support error.
1) spaces in the compilation directory <== this is is you
2) binutils not installed
3) attempting to compile root-owned kernel source directory with unprivileged user

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

Resources