<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > hello內核模塊編譯的全過(guò)程

hello內核模塊編譯的全過(guò)程

作者: 時(shí)間:2011-02-24 來(lái)源:網(wǎng)絡(luò ) 收藏

內核版本:2.6.10

在/home/tmp/下建立兩個(gè)文件hello.c,Makefile

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

MODULE_LICENSE("GPL");

static int hello_init(void)
{
printk(KERN_ALERT "hello module initn");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "hello module exitn");
}

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

clean:
rm -f *.ko *.mod.c *.mod.o *.o

-----------
編譯模塊

#make

清除

#make clean

-----------

為了能夠在終端顯示信息,要修改
/lib/modules/2.6.10/build/include/linux/kernel.h
文件的KERN_ALERT宏。
#define KERN_ALERT "1>"
修改為
#define KERN_ALERT "0>"

------------
安裝模塊

#insmod hello.ko

終端顯示
hello module init

查看已安裝的模塊
#lsmod

卸載模塊

#rmmod hello

終端顯示
hello module exit

-----------
有以下幾點(diǎn)要注意:
1,hello.c文件中調用的頭文件
init.h中的module_init(),module_exit()
kernel.h中的printk(),KERN_ALERT
module.h中的MODULE_LICENSE()

2,Makefile文件中的核心是
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
1),-C $(KERNELDIR)
表示在$(KERNELDIR)目錄下執行make命令。
2),M=$(PWD)
表示包含$(PWD)下的Makefile文件。
3),modules
表示模塊編譯。
4), 用到了ifneq...else...endif語(yǔ)句
由于開(kāi)始還沒(méi)定義KERNELRELEASE,所以只能執行else分支。
而在執行
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
后,會(huì )在內核的Makefile中定義KERNELRELEASE,當進(jìn)入本Makefile時(shí),
則只會(huì )執行ifneq的第一個(gè)分支,即
obj-m := hello.o
這一句話(huà)是非常重要的。事實(shí)上,這個(gè)Makefile做的本份工作就是它。
我們也可以用命令行的方式來(lái)編譯:

在Makefile中的內容寫(xiě)為:
obj-m := hello.o
然后在終端敲入:
#make -C /lib/modules/2.6.10/build M=/home/tmp modules


linux操作系統文章專(zhuān)題:linux操作系統詳解(linux不再難懂)

linux相關(guān)文章:linux教程




評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>