When writing a Linux kernel module, sometimes we need to invoke a function inside a kernel module, however, if it is to compile the generated kernel module under different directories, then a module will call the function of the B module when it is not defined and cannot be called. So before I was in the same directory, write two makefile, and then compile and build two different kernel modules, this way can normally implement a module called B module inside the function, but very troublesome. This blog post will be a way to generate multiple kernel modules at the same time, do not compile the aspect, the following post source code:
Kernel module Cal.ko:
#include <linux/module.h> #include <linux/init.h>module_license ("GPL"); int add (int a, int b) { return a +b;} int sub (int a, int b) {return a-a ;} static int Sym_init () { return 0;} static int sym_exit () { return 0;} Module_init (Sym_init); Module_exit (Sym_exit); Export_symbol (add); Export_symbol (sub);
Kernel module Hello.ko
#include <linux/module.h> #include <linux/init.h>module_license ("GPL"); Module_author ("David Xie"); Module_description ("Hello World Module"); Module_alias ("A simplest module"); static int age = 10;module_param (age, int, s_irugo)//allow T Add (int a, int b), int sub (int a, int b), static int hello_init (void) { printk ("<0>" "Hello world! age =%d\n", a DD (10, 20));//Call the kernel module Cal.ko inside the Add function return 0;} static void hello_exit (void) { printk ("<0>" "Hello Exit%d\n", Sub (30,10));//Call the Sub function inside the kernel module Cal.ko} Module_init (Hello_init); Module_exit (Hello_exit);
Makefile of multiple kernel modules can be generated
Ifneq ($ (kernelrelease),) obj-m: = CAL.O Hello.ocal-objs: = operator.ohello-objs: = Main.oelsekdir: =/lib/modules/ 2.6.32-21-generic/buildall: make-c $ (kdir) m=$ (PWD) Modulesclean: rm-f *.ko *.o *.mod.o *.mod.c *.symvers *.orderendif
Doing make in the current directory now produces Cal.ko and Hello.ko two kernel modules
Linux kernel module function calls