codingfreak
codingfreak

Reputation: 4595

Makefile for a Linux kernel module with multiple sub-directories

I need help regarding the Makefile for a kernel module. Even examples would be of great help.

Currently my module code is under multiple directories. Let us say

<MAIN-DIR> --- l2.c 
    <SUB-DIR1> --- hello.c
    <SUB-DIR2> --- bye.c

For the above scenario how can I code my makefile. Because for building l2 module I need l2.o hello.o and bye.o. And currently they are in multiple directories.

Upvotes: 3

Views: 5729

Answers (1)

Austin Phillips
Austin Phillips

Reputation: 15746

If you're building a module out of the kernel tree, a simple makefile like the following should work:

MODULE_NAME = mymodule

SRC     := foo.c src/bar.c

# Path to target Linux Kernel
KDIR        := $(shell pwd) # <--- Fill in with path to kernel you're compiling against

$(MODULE_NAME)-objs = $(SRC:.c=.o)

obj-m       := $(MODULE_NAME).o
PWD     := $(shell pwd)

EXTRA_CFLAGS := -I$(PWD)/src -I$(PWD)/include

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

As can be seen in the SRC := line, you simple specify the paths to all of your source files, including those in subdirectories. The top level kernel makefile in KDIR will take care of the compilation.

Further information about the kernel build system and out of tree builds can be found in the kernel source documentation in Documentation/kbuild/modules.txt.

Upvotes: 6

Related Questions