- 如果一個模塊需要向其他模塊導出符號(方法或全局變量),需要使用:
- 顯示所有導出的符號
- 應用步驟
- 在模塊函數定義之後使用EXPORT_SYMBOL(function_name);
- 在調用該函數的模塊中使用extern對之聲明;
- 首先加載定義該函數的模塊,在家在調用該函數的模塊。
- 代碼
- hello-3.c
/* * hello-3.c - Illustration the difference * between EXPORT_SYMBOL and EXPORT_SYMBOL_GPL * Include hello-3.c hello-4.c */ # include <linux/kernel.h> /* Wd're doing kernel work */ # include <linux/module.h> /* specifically, a module */ # include <linux/init.h> /* Need for the macros */ void function1(void) { printk(KERN_INFO "Hello world 3\n"); } EXPORT_SYMBOL(function1); void function2(void) { printk(KERN_INFO "Hello world 3 again\n"); } EXPORT_SYMBOL_GPL(function2); static int __init hello_3_init(void) { return 0; } static void __exit hello_3_exit(void) { printk(KERN_INFO "Bye\n"); } module_init(hello_3_init); module_exit(hello_3_exit); MODULE_LICENSE("GPL");
-
hello-4.c
/* * hello-4.c - Illustration the difference * between EXPORT_SYMBOL and EXPORT_SYMBOL_GPL * Include hello-3.c hello-4.c */ # include <linux/kernel.h> /* Wd're doing kernel work */ # include <linux/module.h> /* specifically, a module */ # include <linux/init.h> /* Need for the macros */ extern int function1(void); extern int function2(void); static int __init hello_4_init(void) { function1(); // function2(); return 0; } static void __exit hello_4_exit(void) { printk(KERN_INFO "Goodbye\n"); } module_init(hello_4_init); module_exit(hello_4_exit); //MODULE_LICENSE("GPL");
-
Makefile文件
obj-m += hello-3.o
obj-m += hello-4.o
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
[root@localhost test]# insmod hello-4.ko
[root@localhost test]# rmmod hello-3.ko
[root@localhost test]# dmesg | tail -10
Hello world 3 again
Goodbye
Bye
insmod: error inserting 'hello-4.ko': -1 Unknown symbol in module
ERROR: Module hello_3 is in use by hello_4