博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

hope_process

感觉好累。。。
  heixia108.cublog.cn

关于作者
    既然目标是地平线

   留给世界的只能是背影
   
|| << >> ||
我的分类


linux那些驱动之一 Hello,world!

#include <linux/init.h>  //注意如果没有包含这个头文件,则编译时产生警告,//insmod hello.ko后也没有输出。
#include <linux/module.h>
 
MODULE_LICENSE("Dual BSD/GPL");
 
static int hello_init(void)
{
        printk(KERN_ALERT"hello,world\n");
        return 0;
 
}
 
 
static void hello_exit(void)
{
 
        printk(KERN_ALERT"Goodbye,cruel world\n");
 
}
 
module_init(hello_init);
module_exit(hello_exit);


说明: 与2.4 内核驱动的不同之处在于:
 1、 使用新的入口

必须包含

module_init(your_init_func);

module_exit(your_exit_func);

老版本:
        int init_module(void);

       void cleanup_module(voi);

2.4中两种都可以用,对如后面的入口函数不必要显示包含任何头文件。

2、 GPL

MODULE_LICENSE("Dual BSD/GPL");

老版本:MODULE_LICENSE("GPL");

Makefile 的编写

(NOTE:ifneq后面一定要加一个空格,要不然不能编译成功!)

 


ifneq ($(KERNELRELEASE),)
obj-m := hello.o
 
else
 
        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
        PWD :=$(shell pwd)
default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
 
endif


make 之后生成了如下文件:
[root@hujunlinux drivers]# ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  makefile






这里说一下
 在 结构体的初试化时,2.6的kernel ,gcc开始采用ANSI C的struct结构体的初始化形式:

static struct some_structure = {

.field1 = value,

.field2 = value,

...

};

老版本:非标准的初试化形式

static struct some_structure = {

field1: value,

field2: value,

...

};

试验了一下:
                                               
                                                                              
                                                                              



#include <stdio.h>
                                                                              
int main()
{
    typedef struct my_struct
        {
                int a;
                int b;
        } mystruct ;
                                                                              
        mystruct A={
                    .a=2,
                    .b=3
                  };
   printf("A.a=%d,A.b=%d\n",A.a,A.b);
                                                                              
   return 0;
 } 

//模块参数 练习



#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
 
 
MODULE_LICENSE("Dual BSD/GPL");
 
static char *whom ="world";
static int howmany =1;
 
module_param(howmany,int,S_IRUGO);
module_param(whom,charp,S_IRUGO);
 
 
static int hello_init(void)
{
        int i=howmany;
        for(;i>0;i--)
        {
                printk(KERN_ALERT"hello,%s\n",whom);
        }
        return 0;
 
}
 
 
static void hello_exit(void)
{
 
        printk(KERN_ALERT"Goodbye,cruel world\n");
 
}
 
module_init(hello_init);
module_exit(hello_exit);


转自:http://blog.chinaunix.net/u/24474/showart_217342.html

发表于: 2007-07-14,修改于: 2007-08-20 17:15,已浏览1360次,有评论1条 推荐 投诉


网友评论
网友: 本站网友 时间:2007-07-17 21:31:31 IP地址:58.19.126.★
注:输出在/var/log/messages 中查看

 发表评论