编译内核模块时出现未知类型名称“bool”

时间:2013-08-17 01:30:12

标签: linux-kernel arm linux-device-driver beagleboneblack

我正在尝试在beaglebone(ARM)上为3.8.13编译一个简单的“hello world”内核模块:

的hello.c:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

void init_module(void)
{
        printk(KERN_INFO "My Kernel Module is enabled.\n");
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_INFO "My Kernel Module is disabled.\n");
}

无论我尝试什么,我总是得到

In file included from /home/root/src/moduletest/hello.c:1:0:
./include/linux/init.h:159:1: error: unknown type name 'bool'

我已经尝试重新安装kernel-dev,kernel-headers,“make headers_install”,但没有运气,而且正在运行创意。

这是Makefile:

obj-m += hello.o
KDIR = /usr/src/kernel
PWD := $(shell pwd)

all:
        make -C $(KDIR) M=$(PWD) modules
clean:
        make -C $(KDIR) M=$(PWD) clean

和make的完整输出:

root@beaglebone:~/src/moduletest# make
make -C /usr/src/kernel M=/home/root/src/moduletest modules
make[1]: Entering directory `/usr/src/kernel'
  CC [M]  /home/root/src/moduletest/hello.o
In file included from /home/root/src/moduletest/hello.c:1:0:
./include/linux/init.h:159:1: error: unknown type name 'bool'
/home/root/src/moduletest/hello.c: In function 'init_module':
/home/root/src/moduletest/hello.c:7:9: error: implicit declaration of function 'printk' [-Werror=implicit-function-declaration]
/home/root/src/moduletest/hello.c:7:16: error: 'KERN_INFO' undeclared (first use in this function)
/home/root/src/moduletest/hello.c:7:16: note: each undeclared identifier is reported only once for each function it appears in
/home/root/src/moduletest/hello.c:7:26: error: expected ')' before string constant
/home/root/src/moduletest/hello.c:8:9: warning: 'return' with a value, in function returning void [enabled by default]
/home/root/src/moduletest/hello.c: In function 'cleanup_module':
/home/root/src/moduletest/hello.c:13:16: error: 'KERN_INFO' undeclared (first use in this function)
/home/root/src/moduletest/hello.c:13:26: error: expected ')' before string constant
cc1: some warnings being treated as errors
make[2]: *** [/home/root/src/moduletest/hello.o] Error 1
make[1]: *** [_module_/home/root/src/moduletest] Error 2
make[1]: Leaving directory `/usr/src/kernel'
make: *** [all] Error 2

修改

gcc似乎使用用户空间uapi / linux / types.h标头而不是linux / types.h。添加gcc -v -H显示

GNU C (Linaro GCC 4.7-2013.02-01) version 4.7.3 20130205 (prerelease) (arm-angstrom-linux-gnueabi)
        compiled by GNU C version 4.7.3 20130205 (prerelease), GMP version 5.0.5, MPFR version 3.1.0, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=64 --param ggc-min-heapsize=63851
ignoring duplicate directory "/usr/src/kernel/include"
ignoring duplicate directory "include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /usr/src/kernel/arch/arm/include
 arch/arm/include/generated
 /usr/src/kernel/arch/arm/include/uapi
 arch/arm/include/generated/uapi
 /usr/src/kernel/include/uapi
 include/generated/uapi
 arch/arm/mach-omap2/include
 arch/arm/plat-omap/include
 ./include <-------- this refers to /usr/src/kernel/include and should be above the "uapi" line
End of search list.

这包括树:

. ./include/linux/init.h
.. ./include/linux/compiler.h
... ./include/linux/compiler-gcc.h
.... ./include/linux/compiler-gcc4.h
.. /usr/src/kernel/include/uapi/linux/types.h
... arch/arm/include/generated/asm/types.h
.... /usr/src/kernel/include/uapi/asm-generic/types.h
..... /usr/src/kernel/include/uapi/asm-generic/int-ll64.h
...... arch/arm/include/generated/asm/bitsperlong.h
....... /usr/src/kernel/include/uapi/asm-generic/bitsperlong.h
... /usr/src/kernel/include/uapi/linux/posix_types.h
.... /usr/src/kernel/include/uapi/linux/stddef.h
.... /usr/src/kernel/arch/arm/include/uapi/asm/posix_types.h
..... /usr/src/kernel/include/uapi/asm-generic/posix_types.h
...... arch/arm/include/generated/asm/bitsperlong.h
In file included from /home/root/src/moduletest/hello.c:8:0:
./include/linux/init.h:159:1: error: unknown type name 'bool'
. /usr/src/kernel/include/uapi/linux/module.h
. /usr/src/kernel/include/uapi/linux/kernel.h
.. /usr/src/kernel/include/uapi/linux/sysinfo.h

我不知道为什么会这样做。添加-I / usr / src / kernel / include不起作用,因为它被假定为系统目录,因此被放到包含路径列表的末尾。

1 个答案:

答案 0 :(得分:5)

对于其他任何人来说,我都是通过更换线来实现的。

NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)

中的/ usr / src / kernel / Makefile中的

NOSTDINC_FLAGS += -nostdinc

并使用以下Makefile构建模块:

obj-m+=hello.o
KDIR=/usr/src/kernel
PWD:=$(shell pwd)
ccflags-y=-I /usr/lib/gcc/arm-angstrom-linux-gnueabi/4.7.3/include

all:
        make -C $(KDIR) M=$(PWD) modules
clean:
        make -C $(KDIR) M=$(PWD) clean

这似乎修复了包含路径顺序,并将内核“/ usr / src / kernel / include”放在所有uapi路径之前。