I'm trying to build a module for the Linux Kernel. To explain my problem, I've taken the most simple example : just 2 files, hello.c and Makefile.

#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void) {
    printk(KERN_INFO "init_module() called\n" );
    return 0;

void cleanup_module(void) {
    printk(KERN_INFO "cleanup_module() called\n" );
obj-m += hello.o

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

When I compile with the above makefile, everything is fine.

Now, what I would like to do it to redirect to output files to some other directory in order to avoid polluting my source directory. For this, I use the 'O=' option as explained in the kernel docs like this :

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) O=$(PWD)/objects modules
Unfortunately, I'm unable to compile using this option. Here is the output I get :

make -C /lib/modules/2.6.32-21-generic/build M=/home/username/hello O=/home/username/hello/objects modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-21-generic'

ERROR: Kernel configuration is invalid.
include/linux/autoconf.h or include/config/auto.conf are missing.
Run 'make oldconfig && make prepare' on kernel src to fix it.

WARNING: Symbol version dump /home/username/hello/objects/Module.symvers
is missing; modules will have no dependencies and modversions.

CC [M] /home/username/hello/hello.o
[.....many errors like the folllowing....]
In file included from /usr/src/linux-headers-2.6.32-21-generic/arch/x86/include/asm/percpu.h:45,
from /usr/src/linux-headers-2.6.32-21-generic/arch/x86/include/asm/current.h:5,
from /usr/src/linux-headers-2.6.32-21-generic/arch/x86/include/asm/processor.h:15,
from /usr/src/linux-headers-2.6.32-21-generic/include/linux/prefetch.h:14,
from /usr/src/linux-headers-2.6.32-21-generic/include/linux/list.h:6,
from /usr/src/linux-headers-2.6.32-21-generic/include/linux/module.h:9,
from /home/username/hello/hello.c:1:
/usr/src/linux-headers-2.6.32-21-generic/include/linux/kernel.h:185: warning: conflicting types for built-in function ‘snprintf’
/usr/src/linux-headers-2.6.32-21-generic/include/linux/kernel.h:187: warning: conflicting types for built-in function ‘vsnprintf’
In file included from /usr/src/linux-headers-2.6.32-21-generic/arch/x86/include/asm/string.h:2,
/bin/sh: scripts/basic/fixdep: not found
make[3]: *** [/home/username/hello/hello.o] Error 127
make[2]: *** [_module_/home/username/hello] Error 2
make[1]: *** [sub-make] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-21-generic'
make: *** [all] Error 2
With the V=1 option, I get the following subcall to make that is made :

make -C /home/username/hello/objects \
KBUILD_SRC=/usr/src/linux-headers-2.6.32-21-generic \
KBUILD_EXTMOD="/home/username/hello" -f /usr/src/linux-headers-2.6.32-21-generic/Makefile \

I looked at the main Kernel Makefile and this is the submake command I would expect.

Note that the include/linux/autoconf.h and include/config/auto.conf exist (+ there is no error when not using 'O=' option). After investigation, the "ERROR: Kernel configuration is invalid." can be explained because the current directory is '/home/username/hello/objects' and the search path is relative to this folder...

Has anyone else successfully used the 'O=' option before? Am I missing something?

Many thanks in advance for your help.