33

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

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Adrien
  • 603
  • 1
  • 5
  • 10

6 Answers6

23

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
GroovyDotCom
  • 1,304
  • 2
  • 15
  • 29
Deepak Reddy
  • 351
  • 2
  • 2
  • 1
    Thanks for this answer. I found it to be the most helpful one here. – zmb Mar 09 '16 at 13:46
  • This approach didn't work for me, maybe it depends on the kernel/kbuild version. I did not investigate it further... – MarcusS Oct 21 '20 at 11:06
22

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
Adrien
  • 603
  • 1
  • 5
  • 10
9

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
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Gautham Kantharaju
  • 1,735
  • 1
  • 21
  • 24
  • A useful way to define `KERNELDIR` is using `?=` instead of `:=` so that it defaults to building for the currently running kernel, but can be overridden on the make command line. – Ian Abbott Sep 29 '21 at 17:22
3

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

Mike
  • 47,263
  • 29
  • 113
  • 177
  • 1
    If I add a can.o object in can-objs, i get make[2]: Circular can.o <- can.o dependency dropped. The can.o file won't be link – Adrien Nov 30 '12 at 09:05
0

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 
Yuri
  • 9
  • 1
0

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.

teaholic
  • 61
  • 1
  • 5