日期:2014-05-16  浏览次数:20581 次

linux内核模块编译问题
我在网上copy了别人的一个内核helloworld模块,具体代码如下:
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_ALERT */


int init_module(void)
{
  printk("<1>Hello world 1.\n");

  // A non 0 return means init_module failed; module can't be loaded.
  return 0;
}


void cleanup_module(void)
{
  printk(KERN_ALERT "Goodbye world 1.\n");
}  
makefile如下:
TARGET := hello-1
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc-3.0

${TARGET}.o: ${TARGET}.c

.PHONY: clean

clean:
  rm -rf {TARGET}.o
编译时总出现如下错误:
/lib/modules/2.6.32-34-generic/build/include/linux/bitops.h:17:24: error: asm/bitops.h: 没有那个文件或目录
类似的有好几条,还有
/lib/modules/2.6.32-34-generic/build/include/linux/thread_info.h:82: error: dereferencing pointer to incomplete type
/lib/modules/2.6.32-34-generic/build/include/linux/module.h:319: error: expected specifier-qualifier-list before ‘Elf_Sym’
/lib/modules/2.6.32-34-generic/build/include/linux/module.h:455: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
In file included from hello.c:1:
/lib/modules/2.6.32-34-generic/build/include/linux/module.h:712: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
求高手指教。

------解决方案--------------------
探讨
我在网上copy了别人的一个内核helloworld模块,具体代码如下:
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_ALERT */


int init_module(void)
{
printk("<1>Hello ……

------解决方案--------------------
这个需要安装Linux 源文件,好像在/usr/src/下。并进行链接
------解决方案--------------------
你的源文件和Makefile写的都有一些问题,具体的问题我就不细说了。你可以参考LDD3第22页的源文件,还有第29页的Makefile。刚开始学习的时候,选择书籍和学习资料一定要注意。
------解决方案--------------------
内核模块需要
module_init(xx_init);
module_exit(xx_exit);
MODULE_LICENSE("GPL");

作为入口和出口函数