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
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
I am learning Linux kernel module programming. I am using Beaglebone black for that. I have made simple 'Hello World' application and makefile. I have checked my makefile it's proper. But when I command 'make', it gives me following error:
root#beaglebone:/home/sonu# make
make: Warning: File `Makefile' has modification time 2.2e+02 s in the future
make -C /lib/modules/3.8.13-bone70/build M=/home/sonu modules
make: *** /lib/modules/3.8.13-bone70/build: No such file or directory. Stop.
make: *** [all] Error 2
Though I referred some websites. But, all they are asking me to install packages. As I am a newbie. I don't even know how to configure and start internet connection on Beaglebone using ethernet. Please help me I am stuck. Thanks in advance.
Code is:
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
static int __init hello(void)
{
printk(KERN_INFO "Hello World!");
return 0;
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "Bye");
}
module_init(hello);
module_exit(hello_cleanup);
Makefile is:
obj-m+=Hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
$(CC) Hello.c -o test
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
rm test
It appears that you're trying to build your module for a kernel version that you haven't installed the headers for.
Instead of calling uname directly in your rules, it's helpful to put that into a variable you can override:
obj-m+=Hello.o
KSRC := /lib/modules/$(shell uname -r)/build
all:
make -C $(KSRC) M=$(PWD) modules
$(CC) Hello.c -o test
clean:
make -C $(KSRC) M=$(PWD) clean
rm test
Now, you can override with the actual location of your kernel headers:
make KSRC=/usr/src/linux-headers-4.9.2 all
You can simplify the Makefile further with a catch-all rule:
obj-m+=Hello.o
KSRC := /lib/modules/$(shell uname -r)/build
all: modules
$(CC) Hello.c -o test
%:
make -C $(KSRC) M=$(PWD) $#
clean::
make -C $(KSRC) M=$(PWD) clean
$(RM) test
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
I'm writing a custom kernel module (let's call it mod1) that I would like to export functions for use in other modules (let's call those mod2, etc...). When compiling the other modules, I get warning messages about undefined symbols (functions in mod1). Though everything should work fine after loading, I like to compile without warnings. I read that I should add the path to mod1's Module.symvers to KBUILD_EXTRA_SYMBOLS However, I noticed that my Module.symvers file is empty. What am I doing wrong?
Here's a MWE of a module:
#include <linux/module.h>
MODULE_INFO(version, "0.1");
MODULE_AUTHOR("Me");
MODULE_LICENSE("GPL");
int foo(int x) {
return x;
}
EXPORT_SYMBOL_GPL(foo)
And a Makefile:
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
And me running make showing that Module.symvers is empty:
user#host:~/mwe$ make; [[ -s ./Module.symvers ]] || echo "File size is zero!"
make -C /lib/modules/3.13.0-30-generic/build M=/home/user/mwe modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-30-generic'
Building modules, stage 2.
MODPOST 0 modules
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-30-generic'
File size is zero!
I'm not sure what I'm doing wrong and my search for answers has been fruitless thusfar.
Looks like the Makefile is incorrect. Where is the name of the file you want to compile ? Change test.o to whatever your filename.
Makefile:
obj-m += test.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 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.