Firefly开源社区

打印 上一主题 下一主题

编译内核模块示例

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432

编译内核模块示例

发表于 2017-6-13 11:01:11      浏览:9749 | 回复:5        打印      只看该作者   [复制链接] 楼主
    编译内核的时候有两种方式,1:直接编译进内核 2:编译成单独的内核模块insmod。 在这里介绍下单独的内核模块的编译方式,以hello.c进行示范
废话不多说,直接上程序
1. hello.c
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>

  4. static int __init hello_init(void)
  5. {
  6.         printk(KERN_ALERT "hello driver init \n");
  7.         return 0;
  8. }

  9. static void __exit hello_exit(void)
  10. {
  11.         printk(KERN_ALERT "hello driver exit \n");
  12. }

  13. module_init(hello_init);
  14. module_exit(hello_exit);
复制代码
2. Makefile
  1. PWD=$(shell pwd)
  2. KDIR:=/root/exe/firefly/firefly-rk3288-kernel
  3. obj-m:= hello.o

  4. all:
  5.         make ARCH=arm CROSS_COMPILE=/root/exe/firefly/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin/arm-eabi- -C $(KDIR) M=$(PWD) modules
复制代码




回复

使用道具 举报

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432
发表于 2017-6-13 11:05:52        只看该作者  沙发
编译完后将编译生成的hello.ko拷贝到开发板上insmod hello.ko执行即可。
注意:CROSS_COMPILE设置需要按照自己的交叉编译链路径设置,KDIR是内核的路径,照抄是不行的
回复

使用道具 举报

216

积分

0

威望

0

贡献

技术达人

Rank: 2

积分
216
发表于 2017-6-13 21:47:54        只看该作者  板凳
楼主写的不错啊
回复

使用道具 举报

84

积分

0

威望

0

贡献

技术小白

积分
84
发表于 2018-2-27 10:02:12        只看该作者  地板
大神好,同样的代码在ubuntu14.04上编译出错。
hello.c
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>

  4. static int __init hello_init(void)
  5. {
  6.         printk(KERN_ALERT "hello driver init \n");
  7.         return 0;
  8. }

  9. static void __exit hello_exit(void)
  10. {
  11.         printk(KERN_ALERT "hello driver exit \n");
  12. }

  13. module_init(hello_init);
  14. module_exit(hello_exit);
复制代码

Makefile
  1. PWD=$(shell pwd)
  2. obj-m := hello.o  
  3. KDIR :=/home/bingo/firefly-rk3288-kernel
  4. default:  
  5.         make ARCH=arm  CROSS_COMPILE=/home/bingo/Downloads/arm-eabi-4.6/bin/arm-eabi- -C $(KDIR) M=$(PWD) modules
  6. clean:  
  7.         rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers .tmp_versions
复制代码
报错
  1. make ARCH=arm  CROSS_COMPILE=/home/bingo/Downloads/arm-eabi-4.6/bin/arm-eabi- -C /home/bingo/firefly-rk3288-kernel M=/home/bingo/Desktop/hello modules
  2. make[1]: Entering directory `/home/bingo/firefly-rk3288-kernel'

  3.   WARNING: Symbol version dump /home/bingo/firefly-rk3288-kernel/Module.symvers
  4.            is missing; modules will have no dependencies and modversions.

  5.   CC [M]  /home/bingo/Desktop/hello/hello.o
  6. In file included from include/linux/mmzone.h:18:0,
  7.                  from include/linux/gfp.h:4,
  8.                  from include/linux/kmod.h:22,
  9.                  from include/linux/module.h:13,
  10.                  from /home/bingo/Desktop/hello/hello.c:2:
  11. include/linux/page-flags-layout.h:5:30: fatal error: generated/bounds.h: No such file or directory
  12. compilation terminated.
  13. make[2]: *** [/home/bingo/Desktop/hello/hello.o] Error 1
  14. make[1]: *** [_module_/home/bingo/Desktop/hello] Error 2
  15. make[1]: Leaving directory `/home/bingo/firefly-rk3288-kernel'
  16. make: *** [default] Error 2
复制代码

求指点~


回复

使用道具 举报

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432
发表于 2018-3-18 10:02:18        只看该作者  5#
bingo 发表于 2018-2-27 10:02
大神好,同样的代码在ubuntu14.04上编译出错。
hello.c

你的错误提示已经出来了,如下所示:
include/linux/page-flags-layout.h:5:30: fatal error: generated/bounds.h: No such file or directory
我在百度上找到了如下的答案,希望可以帮到你
http://blog.chinaunix.net/uid-24919665-id-1630140.html
回复

使用道具 举报

432

积分

3

威望

0

贡献

技术达人

Rank: 2

积分
432
发表于 2018-3-18 10:02:41        只看该作者  6#
bingo 发表于 2018-2-27 10:02
大神好,同样的代码在ubuntu14.04上编译出错。
hello.c

你的错误提示已经出来了,如下所示:
include/linux/page-flags-layout.h:5:30: fatal error: generated/bounds.h: No such file or directory
我在百度上找到了如下的答案,希望可以帮到
http://blog.chinaunix.net/uid-24919665-id-1630140.html

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表