LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2011, 04:19 AM   #1
little_ai
LQ Newbie
 
Registered: Jun 2011
Posts: 12

Rep: Reputation: Disabled
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.
 
Old 08-17-2011, 04:24 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

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

Last edited by eSelix; 08-17-2011 at 04:27 AM.
 
Old 08-17-2011, 04:30 AM   #3
little_ai
LQ Newbie
 
Registered: Jun 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanking you for replying.
I do have my headers under directory /usr/src/linux-headers-2.6.32-33-generic/include/linux/
 
Old 08-17-2011, 04:34 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by little_ai View Post
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.
 
2 members found this post helpful.
Old 08-17-2011, 05:30 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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
 
1 members found this post helpful.
Old 08-17-2011, 06:18 AM   #6
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
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
 
0 members found this post helpful.
Old 08-17-2011, 07:19 PM   #7
little_ai
LQ Newbie
 
Registered: Jun 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
thank you, guys!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why couldn't the gcc(version4.3.3) be used to compile the gcc(version3.3.2)? snc Linux - Newbie 6 01-24-2010 11:26 AM
Compile and load kernel module automatically after boot? (Intel NIC module) touser Linux - Newbie 3 08-29-2009 08:45 PM
Compile error, 'H5G_obj_t' does not name a type, is gcc 4.3 incompatible with gcc 3 ? samrat_rao Linux - Software 5 03-03-2009 02:28 PM
Initial gcc installation-How to compile gcc itself pgb205 Linux - Software 4 02-26-2008 10:24 PM
How to compile GCC if you don't have GCC? khermans Linux - Software 13 05-12-2004 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration