1、hello world 的源码摘自《Linux设备驱动程序》第三版。
/* * $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $ */#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);
2、交叉编译
Makefile如下
# If KERNELRELEASE is defined, we've been invoked from the# kernel build system and can use its language.CROSS_COMPILE = arm-linux-KERNELDIR= /home/usr/source/kernel/linux-2.6.35CC = $(CROSS_COMPILE)gcc#LD = $(CROSS_COMPILE)ldDEVICE = helloifneq ($(KERNELRELEASE),) obj-m := $(DEVICE).o# Otherwise we were called directly from the command# line; invoke the kernel build system.else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd)default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules# $(MAKE) -C $(KERNELDIR) ARCH=arm M=$(PWD) modulesclean: rm -rf $(DEVICE).o $(DEVICE).mod* Module.symvers modules.order $(DEVICE).koendif
直接编译会出现:unrecognized command line option "-m64" 等错误,经查询是因为交叉编译但没有配置体系结构,这时加上 ARCH=arm ,再 make,又提示:could not read symbols: File in wrong format ,这次是因为连接器没有指定为交叉连接器,加上 LD = $(CROSS_COMPILE)ld 后可以正常编译了。
3、加载与卸载
两种加载方法:insmod、modprobe
卸载:rmmod
我没啥文化,,来求助大家了. 古代的,现在的. 都行