读书人

哪位高手能帮小弟我编译通过2行代码lt;l

发布时间: 2012-02-26 20:19:45 作者: rapoo

谁能帮我编译通过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代码
/**************************************************/
#include <linux/init.h>


#include <linux/module.h>

MODULE_LICENSE( "Dual BSD/GPL ");

static char *whom= "world ";

static int howmany = 1 ;

module_param(howmany, int ,S_IRUGO);

module_param(whom, charp , S_IRUGO);

static int hello_init(void)
{
int i;
for(i=0;i <howmany;i++)
{
printk(KERN_ALERT "Hello,%s\n ",whom);
}

return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye,cruel world\n ");
}

module_init(hello_init);
module_exit(hello_exit);
/**************************************************/
Makefile
/**************************************************/
# If KERNELRELEASE is defined, we 've been invoked from the

# kernel build system and can use its language.

ifneq ($(KERNELRELEASE),)

obj-m := hello.o


# Otherwise we were called directly from the command

# line; invoke the kernel build system.

else


KERNELDIR ?= /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)


default:

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:


endif
/**************************************************/

在linux2.6的内核下

make -C ~/kernel-2.6 M=`pwd` modules

-C的后面~是kernel的源代码路径,注意后面pwd,不是单引 '是tab上面`



[解决办法]
内核模块的编译要专用的命令,编译方法不对,不是makefile文件的问题。

读书人网 >UNIXLINUX

热点推荐