您的位置:首页 > 运维架构 > Linux

linux 模块编译

2016-07-05 11:44 423 查看
 模板编写一个简单的Makefile,如下所示:

obj-m := hello.o

并使用如下命令编译Hello World模块,如下所示:

make -C /usr/src/linux-2.6.15.5/ M=/driver_study/ modules

如果当前处于模块所在的目录,则以下命令与上述命令同等:

make –C /usr/src/linux-2.6.15.5 M=$(pwd) modules

其中-C后指定的是Linux内核源代码的目录,而M=后指定的是hello.c和Makefile所在的目录,编译结果如下所示:

[root@localhost driver_study]# make -C /usr/src/linux-2.6.15.5/

M=/driver_study/ modules

make: Entering directory '/usr/src/linux-2.6.15.5'

CC [M] /driver_study/hello.o

/driver_study/hello.c:18:35: warning: no newline at end of file

Building modules, stage 2.

MODPOST

CC /driver_study/hello.mod.o

LD [M] /driver_study/hello.ko

make: Leaving directory '/usr/src/linux-2.6.15.5'

从中可以看出,编译过程中经历了这样的步骤:先进入Linux 内核所在的目录,并编译出hello.o 文件,运行MODPOST 会生成临时的hello.mod.c 文件,而后根据此文件编译出 hello.mod.o,之后连接hello.o 和hello.mod.o 文件得到模块目标文件hello.ko,最后离开Linux内核所在的目录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: