Kernel module makefile output name - c

I am trying to compile my kernel module.
The .c file name is file1.c, but I need the .ko file name to be mod1.ko.
How can I do that?
My current makefile:
obj-m := mod1.o
KDIR :=/lib/modules/$(shell uname -r)/build
PDW := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PDW) modules
clean:
$(MAKE) -C $(KDIR) M=$(PDW) clean

You should change your first line to something like this:
obj-m += module_name.o
module_name-y := file1.o
Where module_name is the module file name (in this example the output will be module_name.ko) and it will be build from file1.c. You can add more than one source file to the 2nd line, so it could be:
module_name-y := file1.o file2.o file3.o
In this case module_name.ko will be build from file1.c, file2.c and file3.c.
You should read this document if you want to fully understand Linux kernel makefiles. Your problem is described somewhere around line 190.

The solution looks like this:
obj-m += mod1.o
mod1-objs := file1.o
KBUILD_CPPFLAGS += -I$(PWD)/
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Related

kernel module doesn't search for include library when -I is used

I want to build a basic main.c, that is actually a kernel module written in C. I have include header files that are in include/. I want to use GCC -I to make GCC search for the include headers in -Iinclude. However, GCC doesn't seem to understand that, and I have no clue how to debug it.
Tree:
main.c
include
file.h
main.c
#include "file.h"
...
Makefile:
EXTRA_CFLAGS += -Iinclude -Werror -Wall \
-Wno-missing-braces -Wno-error=unused-function
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:
main.c: fatal error: file.h: No such file or directory
That's because make is not actually running in your directory, when it compiles things.
The -C option in this command:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
tells make to change it's current working directory to that path. Thus, include is no longer the include directory in your current directory.
You should write it like this:
EXTRA_CFLAGS += -I'$M/include' -Werror -Wall \
-Wno-missing-braces -Wno-error=unused-function
all:
$(MAKE) -C "/lib/modules/$$(uname -r)/build" M='$(CURDIR)' modules
clean:
$(MAKE) -C "/lib/modules/$$(uname -r)/build" M='$(CURDIR)' clean
You should always use $(MAKE), and never make, when running a sub-make. And using make's shell function is not really needed since a recipe is already running in a shell. And, PWD is just inherited from the calling shell and might be inaccurate.

Why does my Makefile build an non-existent source file?

I have the following makefile which is supposed to build procdriver.c and thus output a .ko file. The issue is that even when I delete procdriver.c my makefile seems to be able to build a procdriver.ko file. I would like to try to find out from where it gets the source file it is building every time.
TARGET = procdriver
obj-m := procdriver.o
procdriver-objs+= gpioLib.o
KDIR:= /home/pi/myRpi/linux
PWD := $(shell pwd)
all: gpioLib.o procdriver.c
echo<<"in all"
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers
gpioLib.o: gpioLib.c gpioLib.h
gcc -c gpioLib.c -o gpioLib.o
clean:
make -C $(KDIR) SUBDIRS=$(PWD) clean
This is the output from the Makefile: http://paste.ubuntu.com/23575533/
EDIT
the entire output of the remake command: http://paste.ubuntu.com/23577274
I think this is the most important bit of output: http://pastebin.com/0vj9aNMg
obj-m := procdriver.o
procdriver-objs+= gpioLib.o
is wrong. It says that the sole object file for the module will be procdriver.o
and that the build system is to generate that one from procdriver-objs
procdriver-objs is initally empty and so becomes = gpioLib.o. So gpioLib.o
is the only object prerequisite of procdriver.o; hence gpioLib.c and gpioLib.h
are the only source prerequisites of procdriver.o. procdriver.c is redundant.
You want (I believe) to make a module from a pair of object files procdriver.o and gpioLib.o,
compiled respectively from the corresponding .c files. The way you need to express that is:
obj-m += my-module-name.o
my-module-name-objs := procdriver.o gpioLib.o
You will have to rethink your filenames to avoid a collision between my-module-name.o
and procdriver.o. Consult the documentation The Linux Kernel Module Programming Guide: 2.7. Modules Spanning Multiple Files.
Much of the content of your makefile is redundant. As far as I can see all it
needs is:
obj-m += my-module-name.o
my-module-name-objs := procdriver.o gpioLib.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
gpioLib.o: gpioLib.h

Writing a makefile for a kernel module

I want to write a Kernel Module and now I have written some files: a.c, b.c, b.h and d.h.
a.c includes b.h and d.h and b.h includes d.h too.
I wrote a Makefile like this:
ifneq ($(KERNELRELEASE),)
mymodule-objs :=a.c b.c
obj-m += a.o b.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
rm -rf *.o *.mod.c *.symvers *order *.markers *.cmd *-
$(MAKE) -C $(KDIR) M=$(PWD)
clean:
rm -rf *.o *.mod.c *.symvers *order *.markers *.cmd *-
endif
But it doesn't work, how should I write a correct Makefile? I want to get a file name x.ko in end and.
After I use the 'make' command, and I use 'insmod' is give me a message:
insmod: ERROR: could not insert module a.ko: Unknown symbol in module
By the way I use Ubuntu 14.10. The kernel is 3.16.0-37-generic
obj-m += a.o b.o
will create two modules, a.ko and b.ko
if you want to create a single module out of both (which I suppose you do because of the line with mymodule-objs), replace that line with
obj-m += mymodule.o
mymodule.o will be built according to mymodule-objs and then turned into mymodule.ko using modpost.
And as said before, you're missing modules in your $(MAKE) line
Try modelling your Makefile after this?
https://unix.stackexchange.com/questions/122095/understanding-a-make-file-for-making-ko-files
You might not need mymodule-objs := a.c b.c. And I think you're missing modules after the $(MAKE) line

Empty Module.symvers file when compiling a module

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

Building a kernel module from several source files which one of them has the same name as the module

Is it possible to build a kernel module from several source files which one of them has the same name as the module?
For example:
I want to build "mymodule.ko" with the following source files:
mymodule.c
mymodule_func.c
This makefile doesn't work:
#Makefile
obj-m += mymodule.o
mymodule-objs := mymodule.o mymodule_func.o
thanks
I found a solution, I placed my source file in a sub folder:
Makefile
src/mymodule.c
src/mymodule_func.c
#Makefile
obj-m += mymodule.o
mymodule-objs := ./src/mymodule.o ./src/mymodule_func.o
all:
make -C $(KERNEL_PATH) M=$(PWD) modules
clean:
make -C $(KERNEL_PATH) M=$(PWD) clean
Proper way to fix in kernel make file would be as:
#
obj-m+= my_module.o
#append other source files except my_module.c which would be include by default
my_module-objs+= src1.o src2.o
As per my understanding it is not possible to have the module name and the source name to be the same. It would be better to provide module name as module.o and use the Makefile for compiling loadable kernel module as shown below,
Makefile
# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
**obj-m := module.o
module-objs := mymodule.o mymodule_func.o**
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
EXTRA_CFLAGS += -DDEBUG
else
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
$(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) clean
You can use TARGET to name your .ko file as I did in this example:
TARGET = can
KDIR = /lib/modules/3.1.10-1.16-desktop/build
PWD := $(shell pwd)
obj-m += $(TARGET).o
can-objs := can_core.o can_open.o can_select.o can_sysctl.o can_write.o \
can_close.o can_ioctl.o can_read.o can_util.o \
can_debug.o can_error.o \
can_async.o can_sim.o
default:
make -C $(KDIR) M=$(PWD) modules
So after the build I ended with a bunch of object files and can.ko
Another solution is create symlink to the file, say:
mymodule.c: ln -sf mymodule.c _mymodule.c
Now, use _mymodule.o as the object name:
mymodule-objs := _mymodule.o
If anyone has stumbled upon this issue while working with Xilinx SoCs and petalinux, note the generated .bb (bitbake) file. Apart from specifying object files in the Makefile:
modulename-objs+= src1.o src2.o
all files (including headers) must be listed in the modulename.bb file's SRC_URI variable.

Resources