做操作系统内核编译模块实验遇到的问题。
Makefile
ifneq ($(KERNELRELEASE),)
# We were called by kbuild
obj-m += clock.o
else # We were called from command line
KDIR := /lib/modules/$(shell uname -r)/build
#KDIR := /home/cynove/src/kernel/linux-source-2.6.31
PWD := $(shell pwd)
default:
@echo ' Building target module 2.6 kernel.'
@echo ' PLEASE IGNORE THE "Overriding SUBDIRS" WARNING'
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
install:
./do_install.sh *.ko
endif # End kbuild check
######################### Version independent targets ##########################
clean:
rm -f -r *.o *.ko .*cmd .tmp* core *.i
clock.c文件
#include <linux/module.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/time.h>
#define BUF_LEN 100
static int read_clock( char *buf_not_used,
char **my_buf_ptr,
off_t offset,
int buf_len,
int *eof_flag,
void *data_not_used )
{
struct timeval ktv;
static char my_buf[BUF_LEN];
if(offset > 0) return 0;
buf_len = BUF_LEN;
*my_buf_ptr = my_buf;
cli();
ktv = xtime;
sti();
sprintf(my_buf, "%ld %ld\n", ktv.tv_sec, ktv.tv_usec);
for(buf_len=0; buf_len < BUF_LEN; buf_len++)
if(my_buf[buf_len] == '\0') break;
eof_flag = 0;
return buf_len;
}
/*开始下面的结构定义不对*/
struct proc_dir_entry clock_proc_file = {
namelen: 5,
name: "clock",
mode: S_IFREG | S_IRUGO,
size: 100,
owner: THIS_MODULE,
read_proc: read_clock,
};
int init_clock(void)
{
//return proc_register(&proc_root, &clock_proc_file);
create_proc_read_entry("clock",0,NULL,read_clock,NULL);
return 0;
}
void exit_clock(void)
{
//proc_unregister(&proc_root, clock_proc_file.low_ino);
remove_proc_entry("clock",&proc_root);
}
module_init(init_clock)
module_exit(exit_clock)
MODULE_LICENSE("GPL"); /*主要是为了去掉加载的时候的一个警告*/
编译时报错
[root@hadoop01 ~]# make
Building target module 2.6 kernel.
PLEASE IGNORE THE "Overriding SUBDIRS" WARNING
make -C /lib/modules/2.6.32-358.el6.x86_64/build SUBDIRS=/root modules
make[1]: Entering directory /usr/src/kernels/2.6.32-358.el6.x86_64'
CC [M] /root/clock.o
/root/clock.c: In function ‘read_clock’:
/root/clock.c:20: error: implicit declaration of function ‘cli’
/root/clock.c:21: error: ‘xtime’ undeclared (first use in this function)
/root/clock.c:21: error: (Each undeclared identifier is reported only once
/root/clock.c:21: error: for each function it appears in.)
/root/clock.c:22: error: implicit declaration of function ‘sti’
/root/clock.c: At top level:
/root/clock.c:36: error: unknown field ‘owner’ specified in initializer
/root/clock.c:36: warning: initialization from incompatible pointer type
/root/clock.c: In function ‘exit_clock’:
/root/clock.c:50: error: ‘proc_root’ undeclared (first use in this function)
make[2]: *** [/root/clock.o] Error 1
make[1]: *** [_module_/root] Error 2
make[1]: Leaving directory/usr/src/kernels/2.6.32-358.el6.x86_64'
make: *** [default] Error 2
原因可能是因为我的linux内核版本是2.6的,我看论坛上其他人的2.4内核可以正常编译。请问我要如何修改程序来适配2.6的内核?
你可以参考下2.6.31其它驱动注册的方式,比如http://lxr.free-electrons.com/source/drivers/block/cciss.c?v=2.6.31
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。