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

  1. #ifndef CONFIGURATION_HEADER_H
  2. #define CONFIGURATION_HEADER_H
  3. // Some code here
  4. #endif

library_header.h

  1. #ifndef LIBRARY_HEADER_H
  2. #define LIBRARY_HEADER_H
  3. #include "configuration_header.h"
  4. // Some code here
  5. #endif

library_header.c

  1. #ifndef LIBRARY_HEADER_C
  2. #define LIBRARY_HEADER_C
  3. #include "library_header.h"
  4. #include <linux/string.h>
  5. // Some code here
  6. #endif

module_initialization.h

  1. #ifndef MODULE_INITIALIZATION_H
  2. #define MODULE_INITIALIZATION_H
  3. // Some code here
  4. #endif

module_initialization.c

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/printk.h>
  4. #include "library_header.h"
  5. #include "module_initialization.h"
  6. // module_init, module_exit functions and the related stuff.
  7. #endif

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

Makefile1:

  1. KDIR := /lib/modules/$(shell uname -r)/build
  2. obj-m += module_initialization.o
  3.  
  4. lib-m += library_header.o
  5.  
  6. all:
  7. make -C $(KDIR) M=$(PWD) modules
  8. clean:
  9. rm *.o
  10. make -C $(KDIR) M=$(PWD) clean
  11.  
  12. .PHONY: all clean

Makefile2:

  1. KDIR := /lib/modules/$(shell uname -r)/build
  2. obj-m += module_initialization.o library_header.o
  3.  
  4. all:
  5. make -C $(KDIR) M=$(PWD) modules
  6. clean:
  7. rm *.o
  8. make -C $(KDIR) M=$(PWD) clean
  9.  
  10. .PHONY: all clean

Makefile3:

  1. KDIR := /lib/modules/$(shell uname -r)/build
  2. MODULE_NAME := module_initialization.o
  3.  
  4. obj-m += $(MODULE_NAME).o
  5.  
  6. $(MODULE_NAME)-objs := library_header.o
  7.  
  8. all:
  9. make -C $(KDIR) M=$(PWD) modules
  10. clean:
  11. make -C $(KDIR) M=$(PWD) clean
  12.  
  13. .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.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training