LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   linux/module.h (https://www.linuxquestions.org/questions/programming-9/linux-module-h-915605/)

devilboy09 11-26-2011 03:55 AM

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?

jhwilliams 11-26-2011 04:32 AM

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


devilboy09 11-26-2011 04:58 AM

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


jhwilliams 11-26-2011 05:23 AM

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.

devilboy09 11-26-2011 07:03 AM

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


jhwilliams 11-26-2011 08:25 AM

You need to insert the Kernel Object (.ko) file, hello.ko. hello.o is intermediary object code.

devilboy09 11-26-2011 10:03 AM

i'm sorry, i didn't get it.what should i do exactly?

jhwilliams 11-26-2011 10:58 AM

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

devilboy09 11-27-2011 01:52 AM

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)?

jhwilliams 11-27-2011 02:21 AM

Run dmesg and you'll see it.

devilboy09 11-28-2011 10:22 AM

tanX

devilboy09 11-30-2011 12:41 PM

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'


jhwilliams 12-02-2011 06:28 AM

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

devilboy09 12-04-2011 10:52 AM

thank you

resetreset 12-05-2011 01:47 AM

Quote:

Originally Posted by jhwilliams (Post 4534412)
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.


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