[Home]Makefile For Kernel 2.6

Last edit: Peter Favrholdt on September 10, 2006 10:50 (3912 days, 10 hours and 29 minutes ago) (diff)
Rtai.Dk | RecentChanges | Preferences | DIAPM RTAI

Axel Weiss wrote the following on the mailinglist:
Since kernel 2.4. modules should be built via a 'make modules' call to the 
kernel sources. Althought for kernels < 2.5 the old way still works, it is 
deprecated and should be replaced. For 2.6. kernels there is no way to 
compile kernel modules like this (well to be exact: they will compile, but 
not insmod).

Some weeks ago I've figured out a Makefile-template that builds kernel modules 
outside the kernel source tree, but uses the kernel source tree for 
compilation, as required. Feel free to use it and send me your improvements.

You should keep in mind that your kernel sources are installed on a 
user-accessable place (and not on /usr/src/linux), and the link 
/lib/modules/`uname -r`/build must exist and point to the current kernel 
source tree (which will be automatically installed by a 'make modules 
install').

Makefile template to compile external linux-kernel modules:

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#
#  Makefile template for external kernel module compilation
#
#  (C) 2004 by Axel Weiss (aweiss@informatik.hu-berlin.de)
#
#  You have to rename the following pseudo-symbols according to your needs:
#  o <module-name>: Prefix of your final module name
#
#  o <module-obj-list>: List of sub-modules (object files) that will be linked
#                 together
#
#  o <export-object-list>: List of object files that export symbols globally
#                 (which means to other modules, not to other sub-modules)
#
#  o EXTRA_CFLAGS: additional compiler options (like macro defs or include
#                 search paths)
#

KDIR         := /lib/modules/$(shell uname -r)/build
PWD          := $(shell pwd)
KERNEL_24    := $(if $(wildcard $(KDIR)/Rules.make),1,0)
EXTRA_CFLAGS := #empty

obj-m        := <module-name>.o
<module-name>-objs := <module-obj-list>

.PHONY: all clean modules_install

ifeq ($(KERNEL_24),0)
ifeq ($(KERNELRELEASE),)
all:
	$(MAKE) -C $(KDIR) M=$(PWD) SUBDIRS=$(PWD)
clean modules_install:
	$(MAKE) -C $(KDIR) M=$(PWD) SUBDIRS=$(PWD) $@
endif # ifeq ($(KERNELRELEASE),)

else  #################### ifeq ($(KERNEL_24),0)

ifneq ($(KERNELRELEASE),)

export-objs := <export-object-list>

include $(KDIR)/Rules.make
<module-name>.o: $(<module-name>-objs)
	$(Q)$(LD) $(LD_RFLAG) -r -o $@ $(<module-name>-objs)
else  # ifneq ($(KERNELRELEASE),)
all:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
	rm -f *.ko *.o .*.cmd .*.o.flags *.mod.c
endif # ifneq ($(KERNELRELEASE),)

endif #################### ifeq ($(KERNEL_24),0)

# end of Makefile template

I have tested this with several kernel versions, however, with SuSE-kernels or 
early 2.6, there may be some warnings during compilation, and clean actually 
does not work with them. I have no problems with kernels 2.4 and 2.6.6.

Edit text of this page | View other revisions | Download Rtai.dk
Search: