I just started learning linux device driver. I just wrote a simple device driver code and tried compiling it but when ever i do a make i get the following error
make: Nothing to be done for `default'
Here is my device driver code.
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Manoj");
MODULE_DESCRIPTION("My First Driver");
static int __init ofd_init ( void ) {
printk (KERN_INFO "ofd registered");
return 0;
}
static int __exit ofd_exit ( void ) {
printk (KERN_INFO "ofd unregistered");
return 0;
}
module_init ( ofd_init );
module _exit ( ofd_exit );
And my Makefile is like this
# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
And in the path /lib/modules/3.2.0-58-generic-pae/build is also present.
can anyone please tell why this particular make file not working ?
obj-m:= hello.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
Before make -C press tab for space
The problem is in the make file.
Give a tab space before the command.
This is a general rule in writing a Makefile.
Change
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
to
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
For more information about the error refer this link.
Related
I am currently trying to learn how to make kernel modules from the 'Linux Device Drivers' book. I have the basic example typed out as follows:
#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("Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
and I am using the following makefile to compile the file above, hello.c
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
When I run the make command, the console output is as follows:
make -C /lib/modules/5.13.0-40-generic/build M=/home/finlay/src/linux-drivers modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-40-generic'
MODPOST /home/finlay/src/linux-drivers/Module.symvers
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-40-generic'
For some reason the only files which are created are the following:
.Module.symvers.cmd
.modules.order.cmd
Module.symvers
modules.order
None of the files created seems to specifically be about the hello.c file. What's very strange is that this same file was working initially to create the hello.ko file however it is no longer working. Any ideas as to why this is not working?
I have now found the solution, I removed some lines from the makefile as I believed them to be redundant as I thought that they were only used if you were directly invoking the makefile from the kernel build directory. The correct makefile is below.
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
Im a windows driver programmer who is a complete newbie in linux kernel development.I have installed linux kernel headers. I am trying my helloworld module in linux kernel.
#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);
following is the code for my module. makefile for my build is
obj-m +=tryout.o
KDIR =/usr/src/linux-headers-4.13.0-37-generic
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.* *.symvers *.order
but im getting
'fatal error: linux/init.h: No such file or directory while making this module'. What could be the possible reason? and how can i resolve it?
Your Makefile is mis-configured. In particular you used SUBDIRS whereas you're supposed to use M and your $(PWD) is meaningless, you should use pwd to be simple (or $$PWD); Here's how you should set it up:
ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
obj-m := tryout.o
# any other c files that you would like to include go into
# yourmodule-y := <here> e.g.:
# tryout-y := tryout-1.o tryout-2.o
else
# normal makefile
KDIR ?= /usr/src/linux-headers-4.13.0-37-generic
# you really should set KDIR up as:
# KDIR := /lib/modules/`uname -r`/build
all::
$(MAKE) -C $(KDIR) M=`pwd` $#
# Any module specific targets go under here
#
endif
Configuring your makefile like this will allow you to simply type make in your module directory and it will invoke the Kernel's kbuild subsystem which will in-turn use the kbuild part of your Makefile.
Read up on https://www.kernel.org/doc/Documentation/kbuild/modules.txt for all the different permutations on how to do this. It comes with examples.
How can I display the absolute file path of ../linux/init.h that the linker deemed resolvable? IE, ! found init.h # /foo/bar/linux/init.h
I tried make VERBOSE=1, it didn't seem to do anything. Do I need to somehow pass a verbose linker argument to make?
#include <linux/init.h>
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
[/home/user/dev/kernel-sandbox/Makefile]
obj-m += lkm.o
all:
sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
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
I am having trouble building helloworld Linux kernel module. I am using VirtualBox from SUN with Ubuntu ISO image that I downloaded from Ubuntu web site. Any help will be greatly appreciated. Bellow are the c code and the error message that I am getting:
The module file is called hellowrld.c and it contains the code below:
#include <linux/module.h> // included for all kernel modules
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/init.h> // included for __init and __exit macros
MODULE_LICENSE("GPL");
static int __init helloworld_init(void)
{
printk(KERN_INFO "Hello world!\n");
return 0;
}
static void __exit helloworld_exit(void)
{
printk(KERN_INFO "Cleaning up module.\n");
}
module_init(helloworld_init);
module_exit(helloworld_exit);
The make file is called makefile.c and it contains the code below:
obj -m += helloworld.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
The error message that I am getting when I run make command is below:
cc makefile.c -o makefile
makefile.c:1:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before '-' token
obj-m helloworld.o
make: *** No targets specified no makefile found. Stop
The right Makefile looks like this ...
obj-m := helloworld.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