How to compile Linux Kernel module from several files?
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
- All Categories
- 177 LFX Mentorship
- 177 LFX Mentorship: Linux Kernel
- 766 Linux Foundation IT Professional Programs
- 378 Cloud Engineer IT Professional Program
- 174 Advanced Cloud Engineer IT Professional Program
- 75 DevOps IT Professional Program - Discontinued
- 7 DevOps & GitOps IT Professional Program
- 101 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- 4 AI & ML Training
- 1 Blockchain & Decentralized Identity Training
- 10 Cloud & Containers Training
- 1 Cybersecurity Training
- 2 DevOps & Site-Reliability Training
- 1 Linux Kernel Development Training
- 1 Networking Training
- 2 Open Source Best Practice Training
- 2 System Administration Training
- 1 System Engineering Training
- 1 Web & Application Development Training
- 796 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 91 Storage
- 770 Linux Distributions
- 81 Debian
- 68 Fedora
- 23 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 116 Mobile Computing
- 20 Android
- 81 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 395 Off Topic
- 121 Introductions
- 30 Study Material
- 997 Programming and Development
- 310 Kernel Development
- 669 Software Development
- 1K Software
- 397 Applications
- 182 Command Line
- 5 Compiling/Installing
- 69 Games
- 318 Installation
- Archived
- 183 Small Talk
- 2 LFD140 Class Forum
- 1.4K LFS258 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)