如何编译一个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

要解决Linux扫码q导致的程序异常,您需要检查您的程序是否正确配置了扫码q,并且您的程序是否能够正确识别扫码q输入的数据。如果您的程序没有正确配置扫码q,您可以尝试重新配置扫码q,或者更新您的程序,以便能够正确识别扫码q输入的数据。


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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存