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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
11-26-2011, 03:55 AM
|
#1
|
|
Member
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 349
Rep:
|
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?
|
|
|
|
11-26-2011, 04:32 AM
|
#2
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
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.
|
|
|
|
11-26-2011, 04:58 AM
|
#3
|
|
Member
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 349
Original Poster
Rep:
|
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
|
|
|
|
11-26-2011, 05:23 AM
|
#4
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
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.
|
|
|
|
11-26-2011, 07:03 AM
|
#5
|
|
Member
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 349
Original Poster
Rep:
|
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
|
|
|
|
11-26-2011, 08:25 AM
|
#6
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
You need to insert the Kernel Object (.ko) file, hello.ko. hello.o is intermediary object code.
|
|
|
1 members found this post helpful.
|
11-26-2011, 10:03 AM
|
#7
|
|
Member
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 349
Original Poster
Rep:
|
i'm sorry, i didn't get it.what should i do exactly?
|
|
|
|
11-26-2011, 10:58 AM
|
#8
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
hello.o is not the file you want to insert. You want to insert the kernel module, which is hello.ko.
|
|
|
|
11-27-2011, 01:52 AM
|
#9
|
|
Member
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 349
Original Poster
Rep:
|
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)?
|
|
|
|
11-27-2011, 02:21 AM
|
#10
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
Run dmesg and you'll see it.
|
|
|
|
11-28-2011, 10:22 AM
|
#11
|
|
Member
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 349
Original Poster
Rep:
|
tanX
|
|
|
|
11-30-2011, 12:41 PM
|
#12
|
|
Member
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 349
Original Poster
Rep:
|
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'
|
|
|
|
12-02-2011, 06:28 AM
|
#13
|
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,167
|
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.
should read:
|
|
|
|
12-04-2011, 10:52 AM
|
#14
|
|
Member
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 349
Original Poster
Rep:
|
thank you
|
|
|
|
12-05-2011, 01:47 AM
|
#15
|
|
Senior Member
Registered: Mar 2008
Location: India
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,320
Rep:
|
Quote:
Originally Posted by jhwilliams
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:06 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|