2
votes

I am trying to compile a simple "hello world" kernel module for 3.8.13 on a beaglebone (ARM):

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");
}

Whatever I try, I always get

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

I have tried re-installing kernel-dev, kernel-headers, "make headers_install", but no luck, and am running of ideas.

This is the 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

and the full output of 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

Edit:

It appears that gcc uses the user-space uapi/linux/types.h headers instead of linux/types.h. Adding gcc -v -H shows

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.

and this include tree:

. ./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 have no idea why it does this. Adding -I /usr/src/kernel/include does not work as it is assumed to be a system directory and thus gets put to the end of the include path list.

1
Straight up C doesn't have a bool type I believe. Use an int. en.wikipedia.org/wiki/…NG.
possible duplicate of Is bool a native C type? In particular, see this answer, which is relevant to the Linux kernel (which defines its own bool type rather than using the C99 standard <stdbool.h>). Just add #include <linux/types.h>Keith Thompson
<linux/init.h> does include <linux/types.h>, which #defines bool for kernel modules. I narrowed it down, see the edit.slumber

1 Answers

5
votes

For anyone else banging their head, I got it to work by replacing the line

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

in /usr/src/kernel/Makefile by

NOSTDINC_FLAGS += -nostdinc

and using the following Makefile for building the modules:

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

This appears to fix the include path order and puts the kernel "/usr/src/kernel/include" before all the uapi paths.