简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM