LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-26-2011, 03:55 AM   #1
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Rep: Reputation: 7
linux/module.h


i'm going to learn kernel programming based on LLD2 book.
there is a hello module example in the chapter 2 that i can't compile it with gcc.
here's the code:
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"); }
when i'm gonna compile it, i get this error:
Code:
root@ubuntu:/home/devilboy/Desktop# gcc -c hello.c 
hello.c:2:26: fatal error: linux/module.h: No such file or directory
compilation terminated.
how can i install module.h library and where should it be placed?
 
Old 11-26-2011, 04:32 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
Hi Devil Boy,

You need to build the module within the Context of the Linux tree. By default, the compiler will look for user-space headers in /usr/include. There ARE some linux headers there (/usr/include/linux), but module.h is not one of them, as it deals with kernel constructs directly.

In short, you need a Makefile. Also, get rid of the #define MODULE. save the following to a file called Makefile and run make:

Code:
obj-m += foo.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
This triggers the kernel build system.

Given that you are using Ubuntu, you probably already have the kernel headers installed at /usr/src/linux-headers-$(uname -r). module.h lives here:

Code:
jameson@aqua:~$ ls /usr/src/linux-headers-$(uname -r)/include/linux/module.h
/usr/src/linux-headers-3.0.0-12-generic/include/linux/module.h

Last edited by jhwilliams; 11-26-2011 at 04:39 AM.
 
Old 11-26-2011, 04:58 AM   #3
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Original Poster
Rep: Reputation: 7
i did what you said i created a directory named header and i put the Makefile in it, but i got this error:
Code:
root@ubuntu:/home/devilboy/Desktop/Header# make
make -C /lib/modules/3.0.0-12-generic/build M=/home/devilboy/Desktop/Header modules
make[1]: Entering directory `/usr/src/linux-headers-3.0.0-12-generic'
make[2]: *** No rule to make target `/home/devilboy/Desktop/Header/foo.c', needed by `/home/devilboy/Desktop/Header/foo.o'.  Stop.
make[1]: *** [_module_/home/devilboy/Desktop/Header] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-12-generic'
make: *** [all] Error 2
 
Old 11-26-2011, 05:23 AM   #4
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
Ah, your module is called hello.c, not foo.c. You'd need to change that one first line of the makefile, from foo.o to hello.o.
 
Old 11-26-2011, 07:03 AM   #5
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Original Poster
Rep: Reputation: 7
i did what you said and i could compile it . but i can't insert it as module:
Code:
root@ubuntu:/home/devilboy/Desktop# insmod ./hello.o 
insmod: error inserting './hello.o': -1 Invalid module format
 
Old 11-26-2011, 08:25 AM   #6
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
You need to insert the Kernel Object (.ko) file, hello.ko. hello.o is intermediary object code.
 
1 members found this post helpful.
Old 11-26-2011, 10:03 AM   #7
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Original Poster
Rep: Reputation: 7
i'm sorry, i didn't get it.what should i do exactly?
 
Old 11-26-2011, 10:58 AM   #8
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
hello.o is not the file you want to insert. You want to insert the kernel module, which is hello.ko.
Code:
insmod hello.ko
 
Old 11-27-2011, 01:52 AM   #9
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Original Poster
Rep: Reputation: 7
i did insert the hello.ko but nothing happend!!!
Code:
root@ubuntu:/home/devilboy/Desktop# insmod ./hello.ko 
root@ubuntu:/home/devilboy/Desktop#
shouldn't it show me a message(Hello, world)?
 
Old 11-27-2011, 02:21 AM   #10
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
Run dmesg and you'll see it.
 
Old 11-28-2011, 10:22 AM   #11
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Original Poster
Rep: Reputation: 7
tanX
 
Old 11-30-2011, 12:41 PM   #12
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Original Poster
Rep: Reputation: 7
i'm having the same problem with init.h
hello.c:
Code:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
gcc -c hello.c:
Code:
root@ubuntu:/home/devilboy/Desktop# gcc -c hello.c 
hello.c:1:24: fatal error: linux/init.h: No such file or directory
compilation terminated.
i created the Makefile like this
Code:
obj-m += hello.c
all:
		make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
		make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make Makefile:
Code:
root@ubuntu:/home/devilboy/Desktop# make
make -C /lib/modules/3.0.0-12-generic/build M=/home/devilboy/Desktop modules
make[1]: Entering directory `/usr/src/linux-headers-3.0.0-12-generic'
scripts/Makefile.build:310: target `/home/devilboy/Desktop/hello.c' doesn't match the target pattern
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-12-generic'
 
Old 12-02-2011, 06:28 AM   #13
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
You shouldn't be running gcc directly. That's the problem. Just run make. Also, that won't work at this point because you have an error in your makefile.

Code:
obj-m += hello.c
should read:

Code:
obj-m += hello.o
 
Old 12-04-2011, 10:52 AM   #14
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Original Poster
Rep: Reputation: 7
thank you
 
Old 12-05-2011, 01:47 AM   #15
resetreset
Senior Member
 
Registered: Mar 2008
Location: India
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,320

Rep: Reputation: 51
Quote:
Originally Posted by jhwilliams View Post
You need to insert the Kernel Object (.ko) file, hello.ko. hello.o is intermediary object code.
I'd love to find out a bit more about this - so there are TWO files generated then? What then, is "hello.o", and why is it generated?


Thnx.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] partport module is seing my parallel port but lp module doesn´t found any device... fcintron Linux - Hardware 3 03-07-2011 09:45 AM
Compile and load kernel module automatically after boot? (Intel NIC module) touser Linux - Newbie 3 08-29-2009 08:45 PM
error: -1 Invalid module format when using insmod with module cross-compiled for arm AndrewShanks Linux - Embedded & Single-board computer 4 10-15-2007 03:50 AM
kcmshell module, Background&Screensaver Module can't be launch (but works @LookNFeeL) bobbyjoe Linux - Software 0 01-08-2005 04:48 AM
'Invalid module format' loading simple module on Suse Linux Professional 9.1 rocketdude Linux - Distributions 3 07-27-2004 11:40 PM


All times are GMT -5. The time now is 01:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration