被调用模块
add_sub.c
#include
#include
#include "add_sub.h"
long add_int(long a,long b)
{
return a+b;
}
long sub_int(long a,long b)
{
return a-b;
}
EXPORT_SYMBOL(add_int);
EXPORT_SYMBOL(sub_int);
MODULE_LICENSE("Dual BSD/GPL");
add_sub.h
#ifndef _ADD_SUB_H_
#define _ADD_SUB_H_
long add_int(long a,long b);
long sub_int(long a,long b);
#endif
Makefile
obj-m := add_sub.o
KDIR :=/home/sink/work/src/linux/linux-2.6.38
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean
测试调用模块
test.c
#include
#include
#include "add_sub.h"
static long a =1;
static long b =2;
static int AddOrSub = 1;
#define ADD 1
#define SUB 2
static int test_init(void)
{
long result = 0;
printk(KERN_ALERT "test init\n");
if(ADD==AddOrSub)
{
result = add_int(a,b);
}
else
{
result = sub_int(a,b);
}
printk("The result = %d",result);
return 0;
}
static void test_exit(void)
{
printk("exit test!\n");
}
module_init(test_init);
module_exit(test_exit);
module_param(a,long,S_IRUGO);
module_param(b,long,S_IRUGO);
module_param(AddOrSub,int,S_IRUGO);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION("V1.0");
Makefile
obj-m := test.o
KDIR :=/home/sink/work/src/linux/linux-2.6.38
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean