一个简单的驱动例子--insmod 有误,求助!
code:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <asm/errno.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <asm/unistd.h>
#include <linux/types.h>
#include <sys/syscall.h>
#include <linux/dirent.h>
static int driver_open(struct inode *i, struct file *f)
{
printk("<1> open function! n");
return 0;
}
static struct file_operations fops = {
open:driver_open,
};
int my_init1(void)
{
if (register_chrdev(30,"p182", &fops))
return -EIO;
return 0;
}
void my_cleanup1(void)
{
unregister_chrdev(30,"p182");
}
module_init(my_init1);
module_exit(my_cleanup1);
Makefile:
KERNELDIR = /usr/src/linux-2.4.20-8
LD = gcc
CFLAGS=-D__KERNEL__ -I$(KERNELDIR)/include -o -Wall
p182.o : p182.c
$(LD) $(CFLAGS) -c $^ -o $@
clean:
rm -f *.o *.core
我的内核版本是2.4.20-8
编译通过,但是insmod p182.o 后,出现提示:
[root@songyw root]# make
gcc -D__KERNEL__ -I/usr/src/linux-2.4.20-8/include -o -Wall -c p182.c -o p182.o[root@songyw root]# insmod p182.o
p182.o: couldn't find the kernel version the module was compiled for
[root@songyw root]#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
复制代码
改为
复制代码
试试?
还是不行!!!!!
你没有包含:#include <linux/config.h>
把这个包含上就好了。
还是上面的例子,修改了几处结果就出错了!
code:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <asm/errno.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <asm/unistd.h>
#include <linux/types.h>
#include <sys/syscall.h>
#include <linux/dirent.h>
MODULE_AUTHOR("SONGYW "); /* 增加的*/
MODULE_LICENSE("SONGYW "); /*增加的*/
static int driver_open(struct inode *i, struct file *f)
{
printk("<1> open function! n");
return 0;
}
static struct file_operations fops11 = {
open : driver_open,
};
int my_init1(void)
{
if (register_chrdev(30,"p182", &fops11))
return -EIO;
return 0;
}
void my_cleanup1(void)
{
unregister_chrdev(30,"p182");
}
module_init(my_init1);
module_exit(my_cleanup1);
Makefile:
KERNELDIR = /usr/src/linux-2.4.8-20
LD = gcc
CFLAGS=-D__KERNEL__ -I$(KERNELDIR)/include -o -Wall
p182.o : p182.c
$(LD) $(CFLAGS) -c $^ -o $@
clean:
rm -f *.o *.core
/////////////////////////////////////////////////////////////
make后:
[root@songyw root]# make
gcc -D__KERNEL__ -I/usr/src/linux-2.4.8-20/include -o -Wall -c p182.c -o p182.oIn file included from /usr/include/linux/fs.h:23,
from p182.c:6:
/usr/include/linux/string.h:8:2: warning: #warning Using kernel header in userland!
p182.c:17: warning: `struct file' declared inside parameter list
p182.c:17: warning: its scope is only this definition or declaration, which is probably not what you want
p182.c:17: warning: `struct inode' declared inside parameter list
p182.c:23: variable `fops11' has initializer but incomplete type
p182.c:24: unknown field `open' specified in initializer
p182.c:24: warning: excess elements in struct initializer
p182.c:24: warning: (near initialization for `fops11')
p182.c:23: storage size of `fops11' isn't known
make: *** [p182.o] Error 1
求助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
郁闷!!!!!!!!!!!!!!!!!!!!!!!!!!
有的
[root@songyw linux]# cat version.h
#include <linux/rhconfig.h>
#if defined(__module__smp)
#define UTS_RELEASE "2.4.20-8smp"
#elif defined(__module__BOOT)
#define UTS_RELEASE "2.4.20-8BOOT"
#elif defined(__module__bigmem)
#define UTS_RELEASE "2.4.20-8bigmem"
#else
#define UTS_RELEASE "2.4.20-8"
#endif
#define LINUX_VERSION_CODE 132116
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << + (c))
看看你的kernel源码目录, 有没有生成include/linux/version.h
如果没有,就先编译内核
还是不行,还是同样的错误!
加一个
复制代码
再试试看