Linux kernel module function calls

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.