, subdir- extra-y, ccflags . , mymodule.
mymodule/Makefile, , (, ).
obj-m ( : hello.ko, hello.o, hello.ko)
hello-objs get hello.ko, : hello.
Makefile (. CONFIG_HELLO_COMPONENT1)
, , Makefile, include, hello-objs ccflags-y ( , obj-*).
CFLAGS_$*.o (. ) cflags, , .
mymodule/Makefile:
ifeq ($(CONFIG_HELLO_COMPONENT1), y)
ccflags-y += -I$(src)/foo/component1 -DCONFIG_HELLO_COMPONENT1
endif
ccflags-y += -I$(src)/foo/component2
obj-m += hello.o
CFLAGS_component2.o += -DTEST_CFLAGS
hello-objs = hello-1.o
ifeq ($(CONFIG_HELLO_COMPONENT1), y)
hello-objs += foo/component1/component1.o
endif
hello-objs += foo/component2/component2.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make make CONFIG_HELLO_COMPONENT1=y.
mymodule/hello-1.c:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#ifdef CONFIG_HELLO_COMPONENT1
#include <component1.h>
#endif
#include <component2.h>
static int __init init_hello(void) {
#ifdef CONFIG_HELLO_COMPONENT1
component1();
#endif
component2();
printk(KERN_INFO "Hello world 1.\n");
return 0;
}
static void __exit cleanup_hello(void) {
printk(KERN_INFO "Goodbye world 1.\n");
}
module_init(init_hello);
module_exit(cleanup_hello);
mymodule/foo/component2/component2.h:
#ifndef COMPONENT2_H
#define COMPONENT2_H
void component2(void);
#endif
mymodule/foo/component2/component2.c:
#include <linux/kernel.h>
void component2(void) {
printk(KERN_INFO "component2\n");
}
mymodule/foo/component1/component1.h:
#ifndef COMPONENT1_H
#define COMPONENT1_H
void component1(void);
#endif
mymodule/foo/component1/component1.c:
#include <linux/kernel.h>
void component1(void) {
printk(KERN_INFO "component1\n");
}