LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   can not compile hello module by gcc (https://www.linuxquestions.org/questions/programming-9/can-not-compile-hello-module-by-gcc-897790/)

little_ai 08-17-2011 04:19 AM

can not compile hello module by gcc
 
Hey guys,
I am new to Linux drive programming. I am trying to learn it from a old book. The first example is to insert and remove a hello world module which is actually doing nothing.
Here is the code of the module.

Code:

#define MODULE

#include <linux/module.h>

int init_module(void)        {printk("<1>Hello, world\n"); return 0; }
void cleanup_module(void) { printk("<1>Goodbye cruel world\n"); }

I named it hello.c

According to the book. I should be able to compile it easily by typing the command:

Code:

root# gcc -c hello.c
That is where i get "hello.c:3:26: error: linux/module.h: No such file or directory"

this is the information about my system by command
Code:

root# uname -r
2.6.32-33-generic

Could anyone helps out? And please tell the reason.

Because i am new to this section. I have no idea how to read or write a make file. For me it is better to stick with the book. Figure out how to make the gcc works in this situation.

Thanks for attention.

eSelix 08-17-2011 04:24 AM

Probably you have to install kernel headers. Which distribution are you using? In Ubuntu this is package named "linux-headers-generic".

little_ai 08-17-2011 04:30 AM

Thanking you for replying.
I do have my headers under directory /usr/src/linux-headers-2.6.32-33-generic/include/linux/

dwhitney67 08-17-2011 04:34 AM

Quote:

Originally Posted by little_ai (Post 4445335)
According to the book. I should be able to compile it easily by typing the command:

Code:

root# gcc -c hello.c

What book is that? Understandably a C Programming book may instruct you to compile a C program with gcc, however it is not that simple with a Linux Kernel Module.

For a Kernel Module, you should create a Makefile; something like:
Code:

obj-m := hello.o    # Module Name is hello.c

KDIR  := /lib/modules/$(shell uname -r)/build

all:
    $(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
    $(MAKE) -C $(KDIR) M=$(PWD) clean

Also, you do NOT need to be logged in as root to compile a Kernel Module. You only need to be root (or use sudo) when you want to insert it into the kernel, or remove it from the kernel.

Aquarius_Girl 08-17-2011 05:30 AM

and also, the following red dots should be replaced by a TAB.
Makefile is TAB sensitive.
Code:

obj-m := hello.o    # Module Name is hello.c

KDIR  := /lib/modules/$(shell uname -r)/build

all:
.......$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
.......$(MAKE) -C $(KDIR) M=$(PWD) clean


knudfl 08-17-2011 06:18 AM

The headers : sudo apt-get install linux-libc-dev libc6-dev
.. will provide /usr/include/linux/<headers.h>

But not module.h : There used to be /usr/include/linux/module.h
for older kernels like 2.6.18. It's an old book.

So you will have to point to /usr/src/linux-headers-2.6.32-33-generic/include/linux/
to use module.h .

When a Makefile (post #4) is used, something like

export CC="gcc -I/usr/src/linux-headers-2.6.32-33-generic/include/linux -I/usr/include"
&& make

little_ai 08-17-2011 07:19 PM

thank you, guys!


All times are GMT -5. The time now is 12:56 AM.