hello.c编译成linux,hello.c内核模块编译 -- linux内核

Linux开发模块,在本机上看调试信息的方法走通了。当前版本2.6.32-32-generic

uname –r

可以查询

这里取module_param()作为例子。

该宏被定义在include/linux/moduleparam.h文件中,具体定义如下:

#define module_param(name, type, perm)

module_param_named(name, name, type, perm)

其中使用了 3个参数:要传递的参数变量名, 变量的数据类型, 以及访问参数的权限。

hello.c

#include

#include

#include

MODULE_LICENSE("Dual BSD/GPL");

static char *flag="world";

static int times = 5;

module_param(times,int,S_IRUSR);

module_param(flag,charp,S_IRUSR);

static int hello_init(void)

{

int i;

for(i=0;i<=times;i++)

{

printk("(%d)hello ,%s\n",i,flag); //KERN_DEBUG

}

return 0;

}

static void hello_exit(void)

{

printk("Goodbye,%s\n",flag); //KERN_DEBUG

}

module_init(hello_init);

module_exit(hello_exit);

这个文件需要编译成模块,采用

Makefile

obj-m:= hello.o

KERNELDIR := /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

default:

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:

rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

1),-C $(KERNELDIR)

表示在$(KERNELDIR)目录下执行make命令。

2),M=$(PWD)

表示包含$(PWD)下的Makefile文件。

3),modules

表示模块编译

在终端中

make

生成hello.ko模块

Insmod hello.ko flag=”daniu” times=5

root@zhangw:/mnt/hgfs/test_curl/core# dmesg -c

[ 9057.070444] Goodbye,\xffffffe2\xffffff80\xffffff9d\xffffff80\xffffff9ddaniu\xffffffe2\xffffff80\xffffff9d\xffffff80\xffffff9d

[ 9059.357777] (0)hello ,daniu

[ 9059.357781] (1)hello ,daniu

[ 9059.357783] (2)hello ,daniu

[ 9059.357784] (3)hello ,daniu

[ 9059.357785] (4)hello ,daniu

[ 9059.357786] (5)hello ,daniu

rmmod hello.ko

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值