HOWTO compile kernel modules for the kernel 2.6: (编译linux2.6版本内核)

本文详细介绍了如何在Linux环境下编译一个名为hello的模块。包括创建Makefile文件、配置内核源码路径、解决编译错误及安装模块的方法。适用于初学者实践Linux设备驱动开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If you want to compile the hello module follow these steps:
Create the Makefile in your directory with the hello.c

obj-m    := hello.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

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

Now do a
make
... and the hello.ko is built.

If you get something like this
# make
make: Nothing to be done for `default'.

you need to install the kernel source and compile the kernel first (run "make" at least to the point until all "HOSTCC scripts/" stuff is done - this will configure your kernel and allows external module compilation). Make sure /lib/modules/$(shell uname -r)/build points to your build directory (most likely /usr/src/linux...).
Another reason for the above error can be, that your browser converted the TAB before $(MAKE) to spaces. Make sure there is a TAB before $(MAKE).

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
 Install it with install.sh:
#!/bin/sh
install -m 644 hello.ko /lib/modules/`uname -r`/kernel/drivers/hello.ko
/sbin/depmod -a

(adjust the /lib/modules path according to your needs)

Now make a
# modprobe hello 

Or if you don't want to install the module, do this:
# insmod ./hello.ko

..and if your system doesn't freeze you've done it right ;-)

For kernel 2.4, the Makefile would look like this:
TARGET	:= modulename
INCLUDE := -I/lib/modules/`uname -r`/build/include
CFLAGS := -O2 -Wall -DMODULE -D__KERNEL__ -DLINUX
CC := gcc

${TARGET}.o: ${TARGET}.c
$(CC) $(CFLAGS) ${INCLUDE} -c ${TARGET}.c




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

MODULE_LICENSE(
"Dual BSD/GPL");

static int hello_init(void)
{
    printk(KERN_ALERT 
"hello, world ");
    
return 0;
}


static void hello_exit(void)
{
    printk(KERN_ALERT 
"goodbye,cruel world ");
}


module_init(hello_init);
module_exit(hello_exit);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值