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

Time:08-16

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:

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

CodePudding user response:

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.

  • Related