做操作系統內核編譯模塊實驗遇到的問題。
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'
/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
make: *** [default] Error 2
原因可能是因為我的linux內核版本是2.6的,我看論壇上其他人的2.4內核可以正常編譯。請問我要如何修改程序來適配2.6的內核?
我在StackOverFlow上找到了答案,重新修改了一下Makefile。多謝各位。