August 28, 2011
Get started with ‘Linux Device Drivers’

If you’re reading the book ‘Linux Device Drivers’, you probably will meet the same problem.

First, you will see this error,

error: linux/module.h: No such file or directory

I f you use your gcc with -v, you will find

#include “…” search starts here:
#include <…> search starts here:
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.4.3/include
 /usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed
 /usr/include
End of search list.
GNU C (Ubuntu 4.4.3-4ubuntu5) version 4.4.3 (i486-linux-gnu)
    compiled by GNU C version 4.4.3, GMP version 4.3.2, MPFR version 2.4.2-p1.
GGC heuristics: —param ggc-min-expand=100 —param ggc-min-heapsize=131072
Compiler executable checksum: 5998ce5f1765e99eea5269f4c1e38d44
1.c:2:25: error: linux/module.h: No such file or directory

You will see module.h is not included within /usr/include.  Instead, it’s under /usr/src/linux-headers-2.6.32-33-generic/include/linux/.

So, how to make gcc check my path instead of default one?

Make your own Makefile:

bj-m += hello-1.o
all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Then type command: make.  You will see:

$ make
make -C /lib/modules/2.6.32-33-generic/build M=/home/lixian/LinuxDeviceDrivers/ex modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-33-generic’
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-33-generic’

Make V=1, then get

test -e include/linux/autoconf.h -a -e include/config/auto.conf || (        \
    echo;                                \
    echo “  ERROR: Kernel configuration is invalid.”;        \
    echo “         include/linux/autoconf.h or include/config/auto.conf are missing.”;    \
    echo “         Run ‘make oldconfig && make prepare’ on kernel src to fix it.”;    \
    echo;                                \
    /bin/false)

But actually, under include/linux exists autoconf.h.

Reference:

http://www.digitalhermit.com/linux/Kernel-Build-HOWTO.html

http://en.tldp.org/LDP/lkmpg/2.6/html/index.html

http://stackoverflow.com/questions/4715259/linux-modpost-does-not-build-anything

Documentation/kbuild/modules.txt

http://www.digitalhermit.com/linux/Kernel-Build-HOWTO.html

(Source: learningdevicedrivers.blogspot.com)