linux 2.6内核编程-Hello world程序

来源:互联网 发布:查公司数据的网站 编辑:程序博客网 时间:2024/05/22 03:33

/**************************************************************

 module program:hello.c

**************************************************************/

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int hello3_data __initdata= 3;
static int __init hello_3_init(void)
{
   printk(KERN_INFO "Hello, world %d/n",hello3_data);
   return 0;
}
static void __exit hello_3_exit(void)
{
   printk(KERN_INFO "Goodbye,world 3/n");
}
module_init(hello_3_init);//insmod 调用
module_exit(hello_3_exit);//rmmod调用

 

/**************************************************************

 Makefile

**************************************************************/

#指定内核的位置
KERNELDIR =/usr/src/kernels/2.6.31.12-174.2.3.fc12.i686.PAE/
#/home/laq/linux-2.6.32.7
# The current directory is passed to sub-makes as argument

# 表示当前源文件所在的目录
PWD := $(shell pwd)

#表示安装到哪里
INSTALLDIR=/lib/modules/$(shell uname -r)/build
#/lib/modules/2.6.31.12-174.2.3.fc12.i686.PAE/kernel/sound/core/

#表示编译器的位置
#CROSS_COMPILE=/home/tekkaman/working/crosstool-gcc410-k26222/gcc-4.1.0-glibc-2.3.2/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu-
CC    = $(CROSS_COMPILE)gcc

#表示生成的目标(2.6内核的目标文件为.ko)但写还是这样的
obj-m :=hello.o

#表示编译内核模块
modules:
    $(MAKE) -w -C $(KERNELDIR) M=$(PWD) modules


#gcc -DMODULE -D__KERNEL__ -c hello1.c
modules_install:
#      cp hello.ko $(INSTALLDIR)
#安装模块 模块实际是被安装在/lib/modules/$(shell uname -r)/extra下  注意这是/lib/modules/2.6.31.12-174.2.3.fc12.i686.PAE/modules.dep还没有变化,需要执行depmod命令
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install
    depmod



#ifneq ($(KERNELRELEASE),)
#        obj-m := HelloModule.o
#else
#        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
#        PWD := $(shell pwd)

#default:
#        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

#endif


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

#一次可以执行多个
PHONY: modules modules_install clean

 

原创粉丝点击