【问题标题】:Unknown type name "bool" when compiling kernel module编译内核模块时未知类型名称“bool”
【发布时间】:2013-08-19 12:07:17
【问题描述】:

我正在尝试在 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");
}

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

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 不起作用,因为它被假定为系统目录,因此被放在包含路径列表的末尾。

【问题讨论】:

  • 我相信 C 没有 bool 类型。使用一个整数。 en.wikipedia.org/wiki/…
  • Is bool a native C type? 的可能重复项特别是,请参阅this answer,它与 Linux 内核相关(它定义了自己的 bool 类型,而不是使用 C99 标准 &lt;stdbool.h&gt;)。只需添加#include &lt;linux/types.h&gt;
  • 确实包含 ,其中 #defines bool 用于内核模块。我缩小了范围,请参阅编辑。

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


【解决方案1】:

对于其他敲头的人,我通过替换线让它工作

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 路径之前。

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    相关资源
    最近更新 更多