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

谁能帮我编译通过2行代码<linux/module.h>,我给100分
一个简单的代码:
//hello.c
#include   <linux/kernel.h>
#include   <linux/module.h>

#ifdef   MODEVERSIONS
#include   <linux/modeversions.h>
#endif

int   init_module()
{
printk( "new   module   init... ");
return   1;
}

int   cleanup_module()
{
printk( "module   is   removed ");
return   0;
}


[root@linux   usr]#   gcc   -o   test_app   hello.c  
hello.c:2:26:   错误:linux/module.h:没有那个文件或目录

[root@linux   usr]#   gcc   -o   test_app   hello.c   -I/usr/src/f6-2.6.18/include/

In   file   included   from   /usr/src/f6-2.6.18/include/linux/bitops.h:9,
                                  from   /usr/src/f6-2.6.18/include/linux/thread_info.h:20,
                                  from   /usr/src/f6-2.6.18/include/linux/preempt.h:9,
                                  from   /usr/src/f6-2.6.18/include/linux/spinlock.h:49,
                                  from   /usr/src/f6-2.6.18/include/linux/module.h:10,
                                  from   hello.c:2:
/usr/src/f6-2.6.18/include/asm/bitops.h:244:   错误:expected   ‘=’,   ‘,’,   ‘;’,   ‘asm’   or   ‘__attribute__’   before   ‘int’
In   file   included   from   /usr/src/f6-2.6.18/include/linux/spinlock.h:56,
                                  from   /usr/src/f6-2.6.18/include/linux/module.h:10,
                                  from   hello.c:2:
/usr/src/f6-2.6.18/include/asm/system.h:306:   错误:expected   declaration   specifiers   or   ‘...’   before   ‘u8’
/usr/src/f6-2.6.18/include/asm/system.h:306:   错误:expected   declaration   specifiers   or   ‘...’   before   ‘u8’
/usr/src/f6-2.6.18/include/asm/system.h:307:   错误:expected   declaration   specifiers   or   ‘...’   before   ‘u16’
/usr/src/f6-2.6.18/include/asm/system.h:307:   错误:expected   declaration   specifiers   or   ‘...’   before   ‘u16’
/usr/src/f6-2.6.18/include/asm/system.h:308:   错误:expected   declaration   specifiers   or   ‘...’   before   ‘u32’
/usr/src/f6-2.6.18/include/asm/system.h:308:   错误:expected   declaration   specifiers   or   ‘...’   before   ‘u32’
/usr/src/f6-2.6.18/include/asm/system.h:   在函数   ‘cmpxchg_386’   中:


------解决方案--------------------
不是用gcc直接编译,需要写在Makefile内然后编译。
#Makefile
obj-m += helloworld.o

#make -C /usr/src/kernel/`uname -r`-i686 SUBDIRS=$PWD modules

------解决方案--------------------
hello.c代码
/*******************************