Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to compile multiple files together when one of them is a Linux Kernel module

There are several files in my folder:
module.c , uart.c , uart.h .

In the file module.c I have #include "uart.h".

How do I make a makefile?
regular makefile:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

obj-m := module.o
KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
echo "bulding module for intel architecture:" 

when I execute make on the makefile above, it gives me an error that it does not know the functions from uart.c, although I have include in the code

>Solution :

To build a Linux kernel module from two (or more) .c files, module.c and uart.c:

ifneq ($(KERNELRELEASE),)

obj-m := mymodule.o
mymodule-objs := module.o uart.o

else

KERNELDIR ?= "/lib/modules/$(shell uname -r)/build"

all:
    $(MAKE) -C "$(KERNELDIR)" M="$(CURDIR)" modules

install:
    $(MAKE) -C "$(KERNELDIR)" M="$(CURDIR)" modules_install

clean:
    $(MAKE) -C "$(KERNELDIR)" M="$(CURDIR)" clean

endif

The above will build a module named mymodule.ko. For kernel modules composed of more than one object file (module.o and uart.o in this case), the name of the built module object file (mymodule.o in this case) must be different from the names of the component object files.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading