免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1385 | 回复: 1
打印 上一主题 下一主题

请教kernel2.6 modules的写法。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-15 16:40 |只看该作者 |倒序浏览
我的系统:
uname -a
2.6.9-34.EL #1 Fri Feb 24 16:44:51 EST 2006 i686 i686 i386 GNU Linux
gcc -v
gcc version 3.4.5 20051201 (Red Hat 3.4.5-2)

代码如下:

  1.     #include <linux/init.h>
  2.     #include <linux/module.h>
  3.     #include <linux/kernel.h>

  4.     MODULE_LICENSE("GPL");
  5.     static int hello_init(void)
  6.     {
  7.         printk("Hello, world\n");
  8.         return 0;
  9.     }

  10.     static void hello_exit(void)
  11.     {
  12.         printk("Goodbye, cruel world\n");
  13.     }

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

编译命令用:

gcc -D__KERNEL__ -DMODULE -I/lib/modules/2.6.9-34.EL/build/include/ -c sample.c

生成sample.o,然后insmod sample.o.
错误:
insmod: error inserting 'sample.o': -1 Invalid module format

在网上搜索一下,类似的问题最终归结2.6kernel的改变,还有一个可能是我的pc是i686,但安装版本选择的是i386 distribution (这个错误不太明确)。请大家指点一下!谢谢。

论坛徽章:
0
2 [报告]
发表于 2006-05-15 17:27 |只看该作者
惭愧!结贴。为啥我刚发贴就找到答案,之前google了2个小时却未果,郁闷!

正确的做法是写一个Makefile,由内核的Kbuild来帮你编译
obj-m := module.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP