Linux kernel 未知类型名称“;“bool”;编译内核模块时

Linux kernel 未知类型名称“;“bool”;编译内核模块时,linux-kernel,arm,linux-device-driver,beagleboneblack,Linux Kernel,Arm,Linux Device Driver,Beagleboneblack,我试图在beaglebone(ARM)上为3.8.13编译一个简单的“hello world”内核模块: 你好,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"); ret

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

你好,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");
}
我尝试过重新安装内核开发人员、内核头,“makeheaders\u install”,但运气不好,我正在运行一些想法

这是生成文件:

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.

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

对于任何其他人,我通过替换该行使它起作用

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

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

我认为,直起式C没有bool类型。使用int。可能的重复项,特别是,请参阅,它与Linux内核相关(它定义自己的
bool
类型,而不是使用C99标准
)。只需添加
#include
does include,它定义了内核模块的bool。我缩小了范围,请参见编辑。
NOSTDINC_FLAGS += -nostdinc
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