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

Asked by Steven wilson

Beginners question. I'm trying to do a hello.c first compile of a linux driver. I've tried it both on the target platform (Arm BeagleBoard running Ubuntu 10.04) and in a cross-platform environment (Ubuntu 10.10). Basically - the environment isn't set up correctly- and I'm not sure how to fix that?

Can someone please point me in the right direction to get the environment set up correctly?

Thanks!

Steve Wilson
<email address hidden>

---Makefile ----
obj-m += hello.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
----- hello.c -------
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */

int init_module(void)
{
        printk(KERN_INFO "Hello world 1.\n");

        /*
         * A non 0 return means init_module failed; module can't be loaded.
         */
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_INFO "Goodbye world 1.\n");
}
------------ Results -------------------
c hello.c -o hello
hello.c:4:54: error: linux/module.h: No such file or directory
hello.c: In function ‘init_module’:
hello.c:9: error: ‘KERN_INFO’ undeclared (first use in this function)
hello.c:9: error: (Each undeclared identifier is reported only once
hello.c:9: error: for each function it appears in.)
hello.c:9: error: expected ‘)’ before string constant
hello.c: In function ‘cleanup_module’:
hello.c:19: error: ‘KERN_INFO’ undeclared (first use in this function)
hello.c:19: error: expected ‘)’ before string constant
make: *** [hello] Error 1

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu linux Edit question
Assignee:
No assignee Edit question
Solved by:
Steven wilson
Solved:
Last query:
Last reply:
Revision history for this message
mycae (mycae) said :
#1

You will need to install the kernel headers for your current kenrel

sudo apt-get install linux-headers-$(uname -r)

(uname -r prints your current kernel number)

Revision history for this message
Steven wilson (steven-wilson) said :
#2

Unfortunately - that doesn't appear sufficient. Please believe me when I say that I've researched this extensively before I asked the question! I built a kernel using a cross-compiler environment with the ubuntu script "build_kernel.sh"

There are a couple of problems with your solution - the kernel I'm using is 2.6.37-x0.1 which doesn't have a canned set of header files available. So that leaves apt-get out. I DID install the header files in the "deploy" directory of the build. I also copied the entire KERNEL tree over to the ARM proto's file system and pointed the /lib/modules/2.6.37-x0.1/build and kernel pointers to that tree. So apparently - moving the tree over by itself doesn't do the job either.

With all that I get the same result.

I'm currently trying another approach. It to the same kernel and linked it to /usr/src/linux. I also copied the .config file from the original Ubuntu build to this location and chose the "default" configuration that gave me. I'm now running a "make modules" in the /usr/src/linux directory rebuilding the modules...

So any suggestions to where I go from here?

Revision history for this message
mycae (mycae) said :
#3

Well, before you go cross compiling, I would make sure that a simple module like this can be built locally, for your X86-whatever machine. Does this work?

Can you find your linux/module.h manually? have you -I/path/to/linux/header/parent/dir ? (thats a dash capital Eye, not an L).

Have you used GCC's -v switch to check the header search paths?

Revision history for this message
Steven wilson (steven-wilson) said :
#4

Another Dumb Question - do I need to have the source code located somewhere in the linux src directories?

Steve

Revision history for this message
Steven wilson (steven-wilson) said :
#5

mycae - thanks for trying to help - I was in the process of building an X86 version of the kernel to test things on when I receive your question. I then did a google search on "use kbuild to build modules." This took me to the Documentation/kbuild/modules.txt file.

The "secret sauce" that seems to be missing in ALL of the descriptions I found on the net was a pair of `` around the appropriate place, i.e. around the pwd line. For anyone else that runs into this.

Reduce the test case to just:
----------Makefile ---------
obj-m := hello.o
-------------------------------

Use the command:
make -C /usr/src/linux M=`pwd` hello

That was the magic required!