当前位置: 技术问答>linux和unix
求助,编译最简单的“init/cleanup驱动程序”出错
来源: 互联网 发布时间:2016-05-13
本文导语: 我在debian etch上编译一个驱动程序报错,是很简单的一个init/cleanup程序,哪位大哥帮忙看一下吧,先谢谢了。。 源程序(程序应该没有问题,就是《linux程序设计》第三版第十八章的第一个例子程序): #include #i...
我在debian etch上编译一个驱动程序报错,是很简单的一个init/cleanup程序,哪位大哥帮忙看一下吧,先谢谢了。。
源程序(程序应该没有问题,就是《linux程序设计》第三版第十八章的第一个例子程序):
#include
#if defined(CONFIG_SMP)
#define __SMP__
#endif
#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include
#endif
#include
int init_module(void)
{
printk(KERN_DEBUG "Hello, kernel!n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_DEBUG "Good-bye, kernel!n");
}
编译时的命令(也是书上写的编译方法):
gcc -D__KERNEL__ -I/usr/src/linux-headers-2.6.18-4-686/include -DMODULE -Wall -O2 -c hello.c -o hello.o
下面是gcc报的错误:
In file included from /usr/src/linux-headers-2.6.18-4-686/include/asm/thread_info.h:16,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/thread_info.h:21,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/preempt.h:9,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/spinlock.h:49,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/capability.h:45,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:44,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/processor.h:80: error: ‘CONFIG_X86_L1_CACHE_SHIFT’ undeclared here (not in a function)
/usr/src/linux-headers-2.6.18-4-686/include/asm/processor.h:80: error: requested alignment is not a constant
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/rwsem.h:24,
from /usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:42,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:57,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__down_read’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:104: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__down_write_nested’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:156: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__up_read’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:198: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:192: warning: unused variable ‘tmp’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__up_write’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:224: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__downgrade_write’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:249: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:57,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h: In function ‘down’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:105: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h: In function ‘down_interruptible’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:130: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h: In function ‘down_trylock’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:155: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h: In function ‘up’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:179: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
In file included from /usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h:17,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/smp.h:18,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:63,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/mpspec.h:6:25: error: mach_mpspec.h: No such file or directory
In file included from /usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h:17,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/smp.h:18,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:63,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/mpspec.h: At top level:
/usr/src/linux-headers-2.6.18-4-686/include/asm/mpspec.h:8: error: ‘MAX_MP_BUSSES’ undeclared here (not in a function)
/usr/src/linux-headers-2.6.18-4-686/include/asm/mpspec.h:22: error: ‘MAX_IRQ_SOURCES’ undeclared here (not in a function)
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/smp.h:18,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:63,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h:76:26: error: mach_apicdef.h: No such file or directory
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/smp.h:18,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:63,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h: In function ‘hard_smp_processor_id’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h:80: warning: implicit declaration of function ‘GET_APIC_ID’
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h: In function ‘dequeue_signal_lock’:
/usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:1209: warning: implicit declaration of function ‘local_irq_save’
/usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:1211: warning: implicit declaration of function ‘local_irq_restore’
hello.c:12:31: error: linux/modversions.h: No such file or directory
linux-headers-2.6.18-4-686、linux-source-2.6.18包我都装了。另外,我google了一下,据说光安装这些包还不行,还要config?但是具体要怎么做呢?我目前是在自学《linux程序设计》第三版,现在看到第18章设备驱动程序,这个是这章上的第一个例子,目前我暂时还没时间去研究内核的东西,只是想先把这本书看完,先有个基本的概念。。。
源程序(程序应该没有问题,就是《linux程序设计》第三版第十八章的第一个例子程序):
#include
#if defined(CONFIG_SMP)
#define __SMP__
#endif
#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include
#endif
#include
int init_module(void)
{
printk(KERN_DEBUG "Hello, kernel!n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_DEBUG "Good-bye, kernel!n");
}
编译时的命令(也是书上写的编译方法):
gcc -D__KERNEL__ -I/usr/src/linux-headers-2.6.18-4-686/include -DMODULE -Wall -O2 -c hello.c -o hello.o
下面是gcc报的错误:
In file included from /usr/src/linux-headers-2.6.18-4-686/include/asm/thread_info.h:16,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/thread_info.h:21,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/preempt.h:9,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/spinlock.h:49,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/capability.h:45,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:44,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/processor.h:80: error: ‘CONFIG_X86_L1_CACHE_SHIFT’ undeclared here (not in a function)
/usr/src/linux-headers-2.6.18-4-686/include/asm/processor.h:80: error: requested alignment is not a constant
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/rwsem.h:24,
from /usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:42,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:57,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__down_read’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:104: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__down_write_nested’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:156: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__up_read’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:198: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:192: warning: unused variable ‘tmp’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__up_write’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:224: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h: In function ‘__downgrade_write’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/rwsem.h:249: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:57,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h: In function ‘down’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:105: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h: In function ‘down_interruptible’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:130: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h: In function ‘down_trylock’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:155: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h: In function ‘up’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/semaphore.h:179: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’
In file included from /usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h:17,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/smp.h:18,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:63,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/mpspec.h:6:25: error: mach_mpspec.h: No such file or directory
In file included from /usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h:17,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/smp.h:18,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:63,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/mpspec.h: At top level:
/usr/src/linux-headers-2.6.18-4-686/include/asm/mpspec.h:8: error: ‘MAX_MP_BUSSES’ undeclared here (not in a function)
/usr/src/linux-headers-2.6.18-4-686/include/asm/mpspec.h:22: error: ‘MAX_IRQ_SOURCES’ undeclared here (not in a function)
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/smp.h:18,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:63,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h:76:26: error: mach_apicdef.h: No such file or directory
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/smp.h:18,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:63,
from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h: In function ‘hard_smp_processor_id’:
/usr/src/linux-headers-2.6.18-4-686/include/asm/smp.h:80: warning: implicit declaration of function ‘GET_APIC_ID’
In file included from /usr/src/linux-headers-2.6.18-4-686/include/linux/module.h:9,
from hello.c:4:
/usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h: In function ‘dequeue_signal_lock’:
/usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:1209: warning: implicit declaration of function ‘local_irq_save’
/usr/src/linux-headers-2.6.18-4-686/include/linux/sched.h:1211: warning: implicit declaration of function ‘local_irq_restore’
hello.c:12:31: error: linux/modversions.h: No such file or directory
linux-headers-2.6.18-4-686、linux-source-2.6.18包我都装了。另外,我google了一下,据说光安装这些包还不行,还要config?但是具体要怎么做呢?我目前是在自学《linux程序设计》第三版,现在看到第18章设备驱动程序,这个是这章上的第一个例子,目前我暂时还没时间去研究内核的东西,只是想先把这本书看完,先有个基本的概念。。。
|
第一, 你的 这段代码不太对,
第二, 你的 Makefile 不对。
具体原因去 http://blog.csdn.net/pottichu/archive/2007/11/19/1892245.aspx 看看。
|
module_init(init_module);
module_exit(cleanup_module);
我的建议是如果打算看看驱动,就去看LDD 3
linux device driver 3rd
module_exit(cleanup_module);
我的建议是如果打算看看驱动,就去看LDD 3
linux device driver 3rd