hhh_yang 发表于 2013-08-14 23:29

linux设备驱动程序的hello world例子


------------------------------hello.c文件内容------------------------------------
#include <linux/init.h>
#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);

---------------------------Makefile文件内容------------------------------------------
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
make -C /lib/modules/2.6.32-358.14.1.el6.i686/build M=/root/exam_drive/hello modules
make: Entering directory `/usr/src/kernels/2.6.32-358.14.1.el6.i686'
CC /root/exam_drive/hello/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC      /root/exam_drive/hello/hello.mod.o
LD /root/exam_drive/hello/hello.ko.unsigned
NO SIGN /root/exam_drive/hello/hello.ko
make: Leaving directory `/usr/src/kernels/2.6.32-358.14.1.el6.i686'
# ls
hello.c   hello.ko.unsignedhello.mod.oMakefile       Module.symvers
hello.kohello.mod.c      hello.o      modules.order
# insmod ./hello.ko
# rmmod hello
#

make时上面红色的与书上的结果不一样,insmod也没有打印hello word,请问怎么回事?

wwxxxxll 发表于 2013-08-15 10:58

本帖最后由 wwxxxxll 于 2013-08-15 11:10 编辑

dmesg就有了
你的输出级别太低
printk(KERN_ALERT "hello, world\n");
改成
printk(KERN_ERR "hello, world\n");
就有了

有什么问题,可以加入我们群,问题会更快得到回复!
群号:163617970

hhh_yang 发表于 2013-08-25 16:07

谢谢,呵呵

hhh_yang 发表于 2013-08-25 16:07

谢谢,呵呵
页: [1]
查看完整版本: linux设备驱动程序的hello world例子