gpt4 book ai didi

linux-kernel - 编译内核模块时未知类型名称 "bool"

转载 作者:行者123 更新时间:2023-12-02 01:09:58 25 4
gpt4 key购买 nike

我正在尝试在 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”,但没有成功,我正在思考。

这是生成文件:

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

最佳答案

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

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

关于linux-kernel - 编译内核模块时未知类型名称 "bool",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18284241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com