LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1170|回复: 7

linux驱动 编译通不过

[复制链接]
发表于 2007-3-29 16:16:11 | 显示全部楼层 |阅读模式
我照抄“linux设备驱动”程序的hello.c。按书上的 gcc -c hello.c报了一大堆的错。在网上找了个makefile,也没有通过。
/*hello.c*/
#define MODULE
#include <linux/module.h>
int init_module(void){printk("Hello,World\n");return o;}
void cleanup_module(void){printk(Goodbye !\n);}

错误如下:
debian:~/driver# gcc -c hello.c
In file included from /usr/include/linux/sched.h:12,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/linux/jiffies.h:16: error: syntax error before "jiffies_64"
/usr/include/linux/jiffies.h:20: error: syntax error before "get_jiffies_64"
In file included from /usr/include/linux/cpumask.h:8,
                 from /usr/include/linux/sched.h:15,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/linux/bitmap.h: In function `bitmap_empty':
/usr/include/linux/bitmap.h:15: error: `BITS_PER_LONG' undeclared (first use in this function)
/usr/include/linux/bitmap.h:15: error: (Each undeclared identifier is reported o nly once
/usr/include/linux/bitmap.h:15: error: for each function it appears in.)
/usr/include/linux/bitmap.h: In function `bitmap_full':
/usr/include/linux/bitmap.h:29: error: `BITS_PER_LONG' undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function `bitmap_equal':
/usr/include/linux/bitmap.h:44: error: `BITS_PER_LONG' undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function `bitmap_shift_right':
/usr/include/linux/bitmap.h:85: error: `__shr_tmp' undeclared (first use in this  function)
/usr/include/linux/bitmap.h: In function `bitmap_shift_left':
/usr/include/linux/bitmap.h:98: error: `__shl_tmp' undeclared (first use in this  function)
/usr/include/linux/bitmap.h: In function `bitmap_weight':
/usr/include/linux/bitmap.h:144: error: `BITS_PER_LONG' undeclared (first use in  this function)
In file included from /usr/include/linux/sched.h:15,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/linux/cpumask.h: At top level:
/usr/include/linux/cpumask.h:15: error: variable-size type declared outside of a ny function
In file included from /usr/include/linux/sched.h:15,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/linux/cpumask.h: In function `next_online_cpu':
/usr/include/linux/cpumask.h:56: error: structure has no member named `val'
In file included from /usr/include/linux/sched.h:21,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/asm/mmu.h: At top level:
/usr/include/asm/mmu.h:13: error: field `sem' has incomplete type
In file included from /usr/include/asm/smp.h:16,
                 from /usr/include/linux/smp.h:17,
                 from /usr/include/linux/sched.h:23,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/asm/fixmap.h:72: error: `FIX_ACPI_PAGES' undeclared here (not in a function)
/usr/include/asm/fixmap.h:72: error: enumerator value for `FIX_ACPI_END' not int eger constant
/usr/include/asm/fixmap.h:84: error: syntax error before "pgprot_t"
In file included from /usr/include/asm/smp.h:18,
                 from /usr/include/linux/smp.h:17,
                 from /usr/include/linux/sched.h:23,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/asm/mpspec.h:6:25: mach_mpspec.h: 没有那个文件或目录
In file included from /usr/include/asm/smp.h:18,
                 from /usr/include/linux/smp.h:17,
                 from /usr/include/linux/sched.h:23,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/asm/mpspec.h:8: error: `MAX_MP_BUSSES' undeclared here (not in a fu nction)
/usr/include/asm/mpspec.h:9: error: `MAX_MP_BUSSES' undeclared here (not in a fu nction)
/usr/include/asm/mpspec.h:10: error: `MAX_MP_BUSSES' undeclared here (not in a f unction)
/usr/include/asm/mpspec.h:12: error: `MAX_MP_BUSSES' undeclared here (not in a f unction)
/usr/include/asm/mpspec.h:19: error: `MAX_APICS' undeclared here (not in a funct ion)
/usr/include/asm/mpspec.h:20: error: `MAX_MP_BUSSES' undeclared here (not in a f unction)
/usr/include/asm/mpspec.h:20: error: conflicting types for `mp_bus_id_to_type'
/usr/include/asm/mpspec.h:8: error: previous declaration of `mp_bus_id_to_type'
/usr/include/asm/mpspec.h:22: error: `MAX_IRQ_SOURCES' undeclared here (not in a  function)
/usr/include/asm/mpspec.h:24: error: `MAX_MP_BUSSES' undeclared here (not in a f unction)
/usr/include/asm/mpspec.h:24: error: conflicting types for `mp_bus_id_to_pci_bus '
/usr/include/asm/mpspec.h:12: error: previous declaration of `mp_bus_id_to_pci_b us'
/usr/include/asm/mpspec.h:35: error: syntax error before "id"
/usr/include/asm/mpspec.h:36: error: syntax error before "address"
/usr/include/asm/mpspec.h:37: error: syntax error before "id"
/usr/include/asm/mpspec.h:38: error: syntax error before "bus_irq"
/usr/include/asm/mpspec.h:54: error: `MAX_APICS' undeclared here (not in a funct ion)
/usr/include/asm/mpspec.h:54: error: variable-size type declared outside of any function
In file included from /usr/include/asm/smp.h:20,
                 from /usr/include/linux/smp.h:17,
                 from /usr/include/linux/sched.h:23,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/asm/io_apic.h:26: error: syntax error before "u32"
/usr/include/asm/io_apic.h:28: error: syntax error before "u32"
/usr/include/asm/io_apic.h:34: error: syntax error before '}' token
/usr/include/asm/io_apic.h:37: error: syntax error before "u32"
/usr/include/asm/io_apic.h:39: error: syntax error before "u32"
/usr/include/asm/io_apic.h:45: error: syntax error before '}' token
/usr/include/asm/io_apic.h:48: error: syntax error before "u32"
/usr/include/asm/io_apic.h:50: error: syntax error before "u32"
/usr/include/asm/io_apic.h:54: error: syntax error before '}' token
/usr/include/asm/io_apic.h:57: error: syntax error before "u32"
/usr/include/asm/io_apic.h:59: error: syntax error before "u32"
/usr/include/asm/io_apic.h:62: error: syntax error before '}' token
/usr/include/asm/io_apic.h:120: error: `MAX_IRQ_SOURCES' undeclared here (not in  a function)
/usr/include/asm/io_apic.h:120: error: conflicting types for `mp_irqs'
/usr/include/asm/mpspec.h:22: error: previous declaration of `mp_irqs'
In file included from /usr/include/linux/smp.h:17,
                 from /usr/include/linux/sched.h:23,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/asm/smp.h:73:26: mach_apicdef.h: 没有那个文件或目录
In file included from /usr/include/linux/sched.h:23,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/linux/smp.h: In function `on_each_cpu':
/usr/include/linux/smp.h:65: error: invalid type argument of `->'
/usr/include/linux/smp.h:68: error: invalid type argument of `->'
/usr/include/linux/smp.h:68: error: `TIF_NEED_RESCHED' undeclared (first use in this function)
In file included from /usr/include/linux/signal.h:6,
                 from /usr/include/linux/sched.h:25,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/asm/signal.h: At top level:
/usr/include/asm/signal.h:29: error: conflicting types for `sigset_t'
/usr/include/sys/select.h:38: error: previous declaration of `sigset_t'
In file included from /usr/include/linux/sched.h:29,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/linux/completion.h:15: error: syntax error before "wait_queue_head_ t"
/usr/include/linux/completion.h: In function `init_completion':
/usr/include/linux/completion.h:26: error: dereferencing pointer to incomplete t ype
/usr/include/linux/completion.h:27: error: dereferencing pointer to incomplete t ype
In file included from /usr/include/linux/sched.h:30,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/linux/pid.h: At top level:
/usr/include/linux/pid.h:18: error: field `task_list' has incomplete type
/usr/include/linux/pid.h:19: error: field `hash_chain' has incomplete type
/usr/include/linux/pid.h:24: error: field `pid_chain' has incomplete type
In file included from /usr/include/linux/sched.h:102,
                 from /usr/include/linux/module.h:10,
                 from hello.c:9:
/usr/include/linux/timer.h:11: error: field `entry' has incomplete type
In file included from hello.c:9:
/usr/include/linux/module.h:190: error: field `list' has incomplete type
/usr/include/linux/module.h: In function `MOD_INC_USE_COUNT':
/usr/include/linux/module.h:483: error: `KERN_WARNING' undeclared (first use in this function)
/usr/include/linux/module.h:483: error: syntax error before string constant
hello.c: At top level:
hello.c:19: warning: static declaration for `init_module' follows non-static
hello.c:25: warning: static declaration for `cleanup_module' follows non-static
 楼主| 发表于 2007-3-29 16:17:06 | 显示全部楼层
我的内核是2.4.27-2 debian
回复 支持 反对

使用道具 举报

发表于 2007-3-29 19:40:59 | 显示全部楼层
libc6-dev 装了没?
回复 支持 反对

使用道具 举报

发表于 2007-3-30 01:36:33 | 显示全部楼层
你用的是2.4内核的例子,已经不适用于2.6内核了。给个2.6上能用的例子

hello.c
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. static int hello_init(void)
  5. {
  6.         printk(KERN_ALERT "Hello, the world\n");
  7.         return 0;
  8. }
  9. static void hello_exit(void)
  10. {
  11.         printk(KERN_ALERT "Bye, the world!\n");
  12. }
  13. module_init(hello_init);
  14. module_exit(hello_exit);
复制代码

Makefile
  1. obj-m := hello.o
复制代码

最后用 make -C /usr/src/kernel-headers-2.6.8-3-686 M=`pwd` modules 编译

我的机器是 sarge, 内核是 2.6.8-3-686 ,你必须根据自己的情况修改 /usr/src/kernel-headers-2.6.8-3-686 为实际的目录。

当然,你还必须安装了和运行内核一致的 kernel-header, (etch 也许叫什么别的名字)
回复 支持 反对

使用道具 举报

发表于 2007-3-30 09:52:52 | 显示全部楼层
楼上的2.6的Makefile可以修改为较为完整的,其他的模块只要修改*.o就可以了:
# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
        obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
        PWD := $(shell pwd)
default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-3-30 10:24:32 | 显示全部楼层
我内核就是2.4的
如果坚持用2.4的内核应该怎么写makefile
回复 支持 反对

使用道具 举报

发表于 2007-3-30 13:13:33 | 显示全部楼层
Post by dyingfish
我内核就是2.4的
如果坚持用2.4的内核应该怎么写makefile

哦,也许2.4内核只须这样, 你试试看, x y 替换成你具体的目录

gcc -I /usr/src/kernel-headers-2.4.x-y -c hello.c
回复 支持 反对

使用道具 举报

发表于 2007-3-30 15:38:39 | 显示全部楼层
2.4的内核Makefile应该这么写:
TARGET  := hello
WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /usr/src/'uname -r'/include
#INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC      := gcc
       
${TARGET}.o: ${TARGET}.c
clean:
                rm -rf *.o

把上面内容存为Makefile,然后在命令行模式输入make就可以啦!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表