Welcome to the Linux Foundation Forum!

How to compile Linux Kernel module from several files?

Options

I am writing to you because I am having an issue with my module compilation written in C for Kernel and I even can't find documentation which would be helpful for me.

My issue looks like following:

There are several files written in C like this: configuration_header.h

#ifndef CONFIGURATION_HEADER_H
#define CONFIGURATION_HEADER_H
  // Some code here
#endif

library_header.h

#ifndef LIBRARY_HEADER_H
#define LIBRARY_HEADER_H
#include "configuration_header.h"
  // Some code here
#endif

library_header.c

#ifndef LIBRARY_HEADER_C
#define LIBRARY_HEADER_C
#include "library_header.h"
#include <linux/string.h>
  // Some code here
#endif

module_initialization.h

#ifndef MODULE_INITIALIZATION_H
#define MODULE_INITIALIZATION_H
  // Some code here
#endif

module_initialization.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/printk.h>
#include "library_header.h"
#include "module_initialization.h"
  // module_init, module_exit functions and the related stuff.
#endif

And here I am trying to make this using following Makefiles:

Makefile1:

KDIR := /lib/modules/$(shell uname -r)/build
obj-m += module_initialization.o 

lib-m += library_header.o

all: 
    make -C $(KDIR) M=$(PWD) modules
clean:
    rm *.o
    make -C $(KDIR) M=$(PWD) clean

.PHONY: all clean

Makefile2:

KDIR := /lib/modules/$(shell uname -r)/build
obj-m += module_initialization.o library_header.o

all: 
    make -C $(KDIR) M=$(PWD) modules
clean:
    rm *.o
    make -C $(KDIR) M=$(PWD) clean

.PHONY: all clean

Makefile3:

KDIR := /lib/modules/$(shell uname -r)/build
MODULE_NAME := module_initialization.o

obj-m += $(MODULE_NAME).o

$(MODULE_NAME)-objs := library_header.o  

all:
    make -C $(KDIR) M=$(PWD) modules
clean:
    make -C $(KDIR) M=$(PWD) clean

.PHONY: all clean

None of above worked. After compilation according the first version I am getting following error:

ERROR: modpost: "library_header_initialization" [/home/user/Code/C/MemoryReader-v1.a/configuration_test.ko] undefined! make[3]: *** [scripts/Makefile.modpost:145: /home/user/Code/C/MemoryReader-v1.a/Module.symvers] Error 1 make[2]: *** [/usr/src/linux-headers-6.11.0-26-generic/Makefile:1879: modpost] Error 2 make[1]: *** [Makefile:224: __sub-make] Error 2 make[1]: Leaving directory '/usr/src/linux-headers-6.11.0-26-generic' make: *** [Makefile:14: all] Error 2

I in the first moment thought that there is somewhere an error in the program but I have noticed that there is library_header.o file never created so it seemed to me like the code was never compiled properly.

According to the second version of the Makefile the code is compiled if I will modify it somehow, but it will create two .ko modules and this is something what I don't want to do.

How can I write proper Makefile for the code above to get the module successfully compiled? Is there any documentation which would document well Makefiles used for Linux Kernel compilation? I know it should be possible because I saw another code far more complex than mine which seems like it work this way but I didn't find out how the Makefiles of such code works. Btw. I also tried a few advises from AI but the code from it is the third version and it doesn't work either because of the printk functions - resp. their equivalents doesn't write an output to the log (the last version of Makefile is from AI).

I have tried to write various Makefile versions and none worked. Also I have tried to find a documentation but with no success.

Categories

Upcoming Training