1
votes

So I am trying to re-factor a kernel module I have previously written (by removing certain functions into different a different .h/.c file. Here is my current Makefile:


EXTRA_CFLAGS += 
KERNEL_SRC:= /lib/modules/$(shell uname -r)/build
SUBDIR= $(PWD)
GCC:=gcc
RM:=rm

.PHONY : clean

all: clean modules

obj-m:= kernel_module.o

modules:
    $(MAKE) -C $(KERNEL_SRC) M=$(SUBDIR) modules

clean:
$(RM) -f *.ko *.o *.mod.c Module.symvers 

When I try to make it, I get: error: ‘ref_sleep’ undeclared (first use in this function). How do I modify the Makefile to make sure my library is getting compiled, and it is being linked properly to my kernel module? It is worth nothing that the library needs variables defined in the kernel module, and vice versa.

Thanks in advance.

1
Offtopic: you can use the following in your "clean" target instead of $(RM) <...> to remove all files generated during the build: "$(MAKE) -C $(KERNEL_SRC) M=$(SUBDIR) clean". Some files may still remain if "clean" is as it is now. - Eugene

1 Answers

2
votes

If I understand the problem correctly, you would like to build your kernel module (say, my_module.ko) from several source files (e.g., source1.c, source2.c, source3.c). If so, you can do it as follows in the Makefile:

obj-m := my_module.o
my_module-y := source1.o source2.o source3.o