如何编译一个linux下的驱动模块

如何编译一个linux下的驱动模块,第1张

按照《linux设备驱动开发详解》一书中的步骤实现经典例子"hello,world!"的例子。

具体步骤如下:

=============================================

1.源码如下:

/*

* hello.c -- the example of printf "hello world!" in the screen of driver program

*/

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL")/* declare the license of the module ,it is necessary */

static int hello_init(void)

{

printk(KERN_ALERT "Hello World enter!\n")

return 0

}

static int hello_exit(void)

{

printk(KERN_ALERT "Hello world exit!\n")

}

module_init(hello_init)/* load the module */

module_exit(hello_exit)/* unload the module */

进入目录:

[root@Alex_linux /]#cd /work/jiakun_test/moduletest

[root@Alex_linux moduletest]# vi hello.c

然后拷入上面书上的源码。

2.编译代码:

1>.首先我在2.4内核的虚拟机上进行编译,编译过程如下:

[root@Alex_linux moduletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c

其中-I选项指定内河源码,也就是内核源码树路径。编译结果:

hello.c:1:22: net/sock.h: No such file or directory

hello.c: In function `hello_init':

hello.c:6: warning: implicit declaration of function `printk'

hello.c:6: `KERN_ALERT' undeclared (first use in this function)

hello.c:6: (Each undeclared identifier is reported only once

hello.c:6: for each function it appears in.)

hello.c:6: parse error before string constant

hello.c: In function `hello_exit':

hello.c:11: `KERN_ALERT' undeclared (first use in this function)

hello.c:11: parse error before string constant

hello.c: At top level:

hello.c:13: warning: type defaults to `int' in declaration of `module_init'

hello.c:13: warning: parameter names (without types) in function declaration

hello.c:13: warning: data definition has no type or storage class

hello.c:14: warning: type defaults to `int' in declaration of `module_exit'

hello.c:14: warning: parameter names (without types) in function declaration

hello.c:14: warning: data definition has no type or storage class

在网上查询有网友提示没有引入kernel.h

解决:vi hello.c

在第一行加入:#include <linux/kernel.h>

再次编译仍然报KERN_ALERT没有声明

修改编译条件-I,再次编译:

[root@Alex_linux moduletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c

[root@Alex_linux moduletest]#ls

hello.c hello.o Makefile

[root@Alex_linux moduletest]#

2>.接着我尝试在2.6内核的虚拟机上进行编译

编译过程如下:

[root@JiaKun moduletest]# ls

hello.c makefile

[root@JiaKun moduletest]# vi hello.c

[root@JiaKun moduletest]# make

make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moduletest modules

make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.

make: *** [modules] Error 2

[root@JiaKun moduletest]# vi makefile

[root@JiaKun moduletest]# make

make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moduletest modules

make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'

scripts/Makefile.build:17: /home/alex/test/moduletest/Makefile: No such file or directory

make[2]: *** No rule to make target `/home/alex/test/moduletest/Makefile'. Stop.

make[1]: *** [_module_/home/alex/test/moduletest] Error 2

make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'

make: *** [modules] Error 2

[root@JiaKun moduletest]# mv makefile Makefile

[root@JiaKun moduletest]# make

make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moduletest modules

make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'

CC [M] /home/alex/test/moduletest/hello.o

Building modules, stage 2.

MODPOST

CC /home/alex/test/moduletest/hello.mod.o

LD [M] /home/alex/test/moduletest/hello.ko

make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'

[root@JiaKun moduletest]# ls

hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Module.symvers

是这么回事,当你自己要写一个字符设备或者看别人写的是字符设备时,要定义一个字符设备的结构体struct cdev{/*里面是一些字符设备的相关属性,包括file_operations结构体,设备号等等*/},然后调用register_chrdev_region(),申请设备号,再用cdev_add()想内核注册设备,这里,内核就知道你要注册的就是字符设备了,同理,如果是块设备的话用register_blkdev()来注册块设备,经过一系列的初始化后添加add_disk(),内核也就知道你添加的是块设备了

不需要,其实内核都是写好了的各个模块。

你需要了解的是它提供给你的接口。

因为你在嵌入式开发中不可能重新去写文件调度,进程调度,内存管理。

你需要做的是调整这个功能,内核都有相应的开关。

作为驱动研发,主要是将硬件按一定的方式封装给内核,主要要了解内核对硬件的抽象,比如硬件抽象成文件,驱动中需要的各种相关锁。

当然理解这些功能内核是怎么实现对研发是有好处的。

但是介意从 应用 驱动 内核 这样去了解要好一些。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7210803.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-03
下一篇2023-04-03

发表评论

登录后才能评论

评论列表(0条)

    保存