我照书上敲了段代码:
/****************************************************************/
#include
#if defined(CONFIG_SMP)
#define __SMP__
#endif
#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include
#endif
#include
static __init int init_module(void)
{
printk(KERN_DEBUG "Hello, kernel!\n");
return (0);
}
static __exit void cleanup_module(void)
{
printk(KERN_DEBUG "Good-bye, kernel!\n");
}
/****************************************************************/
书上的编译命令是:
gcc -D__KERNEL__ -I /usr/src/linux/include/ -DMODULE -Wall -O2 -c d_hello.c -o hello.o
由于我/usr/src/下没有linux目录,所以我改成了:
gcc -D__KERNEL__ -I /usr/src/kernels/2.6.26.3-29.fc9.i686/include/ -DMODULE -Wall -O2 -c d_hello.c -o hello.o
结果报了一堆错:
In file included from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/prefetch.h:14,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/list.h:6,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:9,
from d_hello.c:9:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h:112: error: ‘CONFIG_X86_L1_CACHE_SHIFT’ undeclared here (not in a function)
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h:112: error: requested alignment is not a constant
In file included from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/prefetch.h:14,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/list.h:6,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:9,
from d_hello.c:9:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h:152:1: warning: "cache_line_size" redefined
In file included from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/pda.h:7,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/current_64.h:7,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/current.h:4,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h:15,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/prefetch.h:14,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/list.h:6,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:9,
from d_hello.c:9:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/cache.h:64:1: warning: this is the location of the previous definition
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h: In function ‘load_cr3’:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h:187: error: ‘CONFIG_PAGE_OFFSETUL’ undeclared (first use in this function)
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h:187: error: (Each undeclared identifier is reported only once
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h:187: error: for each function it appears in.)
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h: At top level:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/processor.h:236: error: requested alignment is not a constant
In file included from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/thread_info.h:5,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/thread_info.h:47,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/preempt.h:9,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/spinlock.h:49,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/seqlock.h:29,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/time.h:8,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/stat.h:60,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:10,
from d_hello.c:9:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/thread_info_64.h: In function ‘current_thread_info’:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/thread_info_64.h:66: error: ‘THREAD_SIZE’ undeclared (first use in this function)
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/thread_info_64.h: In function ‘stack_thread_info’:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/thread_info_64.h:74: error: ‘THREAD_SIZE’ undeclared (first use in this function)
In file included from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/elf.h:7,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:14,
from d_hello.c:9:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h: In function ‘elf_common_init’:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h:166: error: ‘struct pt_regs’ has no member named ‘r8’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h:166: error: ‘struct pt_regs’ has no member named ‘r9’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h:166: error: ‘struct pt_regs’ has no member named ‘r10’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h:166: error: ‘struct pt_regs’ has no member named ‘r11’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h:167: error: ‘struct pt_regs’ has no member named ‘r12’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h:167: error: ‘struct pt_regs’ has no member named ‘r13’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h:167: error: ‘struct pt_regs’ has no member named ‘r14’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/elf.h:167: error: ‘struct pt_regs’ has no member named ‘r15’
In file included from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/mmzone.h:16,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/gfp.h:4,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/slab.h:12,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/percpu.h:5,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/local.h:4,
from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:19,
from d_hello.c:9:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/nodemask.h: In function ‘__first_node’:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/nodemask.h:233: warning: implicit declaration of function ‘find_first_bit’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/nodemask.h: In function ‘__next_node’:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/nodemask.h:239: warning: implicit declaration of function ‘find_next_bit’
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/nodemask.h: In function ‘__first_unset_node’:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/nodemask.h:257: warning: implicit declaration of function ‘find_first_zero_bit’
In file included from /usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:21,
from d_hello.c:9:
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/asm/module.h:70:2: error: #error unknown processor family
d_hello.c: At top level:
d_hello.c:22: error: static declaration of ‘init_module’ follows non-static declaration
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:74: error: previous declaration of ‘init_module’ was here
d_hello.c: In function ‘init_module’:
d_hello.c:24: warning: implicit declaration of function ‘printtk’
d_hello.c: At top level:
d_hello.c:28: error: static declaration of ‘cleanup_module’ follows non-static declaration
/usr/src/kernels/2.6.26.3-29.fc9.i686/include/linux/module.h:75: error: previous declaration of ‘cleanup_module’ was here
请哥哥姐姐们帮我看看应该怎样才能编译通过这段代码
[ 本帖最后由 WillingHeart 于 2008-9-25 23:25 编辑 ]
chris078426 于 2008-09-26 11:07:45发表:
WillingHeart 于 2008-09-25 23:22:52发表:
编过去了,但还是有些不明白,请达人讲解
代码修改如下
/****************************************************************/
#include
#if defined(CONFIG_SMP)
#define __SMP__
#endif
#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include
#endif
#include
static __init int _init_module(void)
{
printk(KERN_DEBUG "Hello, kernel!\n");
return (0);
}
static __exit void _cleanup_module(void)
{
printk(KERN_DEBUG "Good-bye, kernel!\n");
}
/****************************************************************/
一下是Makefile
TARGET = t-driver
OBJS = t-driver.o
MDIR = drivers/misc
EXTRA_CFLAGS = -DEXPORT_SYMTAB
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
DEST = /lib/modules/$(CURRENT)/kernel/$(MDIR)
obj-m := $(TARGET).o
default:
make -C $(KDIR) SUBDIRS=$(PWD) modules
$(TARGET).o: $(OBJS)
$(LD) $(LD_RFLAG) -r -o $@ $(OBJS)
ifneq (,$(findstring 2.4.,$(CURRENT)))
install:
echo $(DEST) $(LD_RFLAG)
su -c "cp -v $(TARGET).o $(DEST) && /sbin/depmod -a"
else
install:
su -c "cp -v $(TARGET).ko $(DEST) && /sbin/depmod -a"
endif
clean:
-rm -f *.o *.ko .*.cmd .*.flags *.mod.c
-include $(KDIR)/Rules.make