2 nikobelic8 nikobelic8 于 2016.03.22 20:07 提问

linux2.6内核编译报错

做操作系统内核编译模块实验遇到的问题。
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的内核?

3个回答

nikobelic8
nikobelic8   2016.03.23 11:19
已采纳

我在StackOverFlow上找到了答案,重新修改了一下Makefile。多谢各位。

oyljerry
oyljerry   Ds   Rxr 2016.03.23 07:47

主要是头文件等要配置正确。定义找不到

nikobelic8
nikobelic8   2016.03.23 11:19

我在StackOverFlow上找到了答案,重新修改了一下Makefile。多谢各位。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!