public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* 'hello world' module
@ 2005-06-11 12:11 Ilan S.
  2005-06-11 18:17 ` Sam Ravnborg
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Ilan S. @ 2005-06-11 12:11 UTC (permalink / raw)
  To: linux-kernel

Hello dear professionals!

I would be very thankful if anybody prompt me what's wrong.
I'm trying to build the "Hello world" module from O'Reilly's "Linux device 
drivers" and that is what I get:

[ilanso@Netvision Kernel]$ make -C /home/ilanso/src/linux-2.6.11.11 M=`pwd`
make: Entering directory `/home/ilanso/src/linux-2.6.11.11'
  Building modules, stage 2.
  MODPOST
make: Leaving directory `/home/ilanso/src/linux-2.6.11.11'
[ilanso@Netvision Kernel]$ ls
hello.c  Makefile
[ilanso@Netvision Kernel]$  

Please email me to ilan_sk@netvision.net.il, because I'm not subscribed to 
this mailing list yet...
Thanks in advance...

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
@ 2005-06-11 15:53 Nick Warne
  0 siblings, 0 replies; 12+ messages in thread
From: Nick Warne @ 2005-06-11 15:53 UTC (permalink / raw)
  To: linux-kernel, ilan_sk

Hi Ilan,

I don't know about the Oh Really! version, but this one taken from 'Beginning 
Linux Programming' (forward by Alan Cox!) ISBN 1-861002-97-1 worked OK for 
me.

2.4.31, GCC 3.4.4

Build like:

gcc -D__KERNEL__ -I/usr/src/linux/include -DMODULE -Wall -O2 -c hello.c -o 
hello.o

Edit hello.c to suit:



#include <linux/module.h>

#if defined(CONFIG_SMP)
#define __SMP__
#endif

#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include <linux/modversions.h>
#endif

#include <linux/kernel.h>

MODULE_AUTHOR ("Nick Warne <nick@linicks.net>");
MODULE_DESCRIPTION ("Hello Kernel! module");
MODULE_LICENSE("GPL");

int init_module(void)
{
        printk(KERN_DEBUG "Hello, kernel!\n");
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_DEBUG "Good-bye, kernel!\n");
}



bash-2.05b# insmod hello.o
bash-2.05b# dmesg | tail -n1
Hello, kernel!

bash-2.05b# lsmod
Module                  Size  Used by    Tainted: P
hello                    320   0  (unused)

bash-2.05b# rmmod hello
bash-2.05b# dmesg | tail -n1
Good-bye, kernel!



Hope that helps.

Nick
-- 
"When you're chewing on life's gristle,
Don't grumble, Give a whistle..."

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
       [not found] <4egz4-2tj-15@gated-at.bofh.it>
@ 2005-06-11 16:25 ` Robert Hancock
  2005-06-11 16:31   ` Jan Engelhardt
  0 siblings, 1 reply; 12+ messages in thread
From: Robert Hancock @ 2005-06-11 16:25 UTC (permalink / raw)
  To: linux-kernel

Nick Warne wrote:
> 2.4.31, GCC 3.4.4
> 
> Build like:
> 
> gcc -D__KERNEL__ -I/usr/src/linux/include -DMODULE -Wall -O2 -c hello.c -o 
> hello.o

That compilation method will not work on 2.6. You have to use the kernel 
makefiles to build the module. See:

http://linuxdevices.com/articles/AT4389927951.html

-- 
Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from hancockr@nospamshaw.ca
Home Page: http://www.roberthancock.com/


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
  2005-06-11 16:25 ` 'hello world' module Robert Hancock
@ 2005-06-11 16:31   ` Jan Engelhardt
  0 siblings, 0 replies; 12+ messages in thread
From: Jan Engelhardt @ 2005-06-11 16:31 UTC (permalink / raw)
  To: Robert Hancock; +Cc: linux-kernel

>> 2.4.31, GCC 3.4.4
>> 
>> Build like:
>> 
>> gcc -D__KERNEL__ -I/usr/src/linux/include -DMODULE -Wall -O2 -c hello.c -o
>> hello.o
>
> That compilation method will not work on 2.6. You have to use the kernel
> makefiles to build the module. See:
>
> http://linuxdevices.com/articles/AT4389927951.html

Time for URLs - http://jengelh.hopto.org/linux/krn_buildenv.php
The best, is of course, working examples like jengelh.hopto.org/f/oops_ko.tbz2 
as suggested in that php url. Though, instead of saying hello world, 
the oops_ko sample mods fault on you. :-)



Jan Engelhardt                                                               
--                                                                            
| Gesellschaft fuer Wissenschaftliche Datenverarbeitung Goettingen,
| Am Fassberg, 37077 Goettingen, www.gwdg.de

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
@ 2005-06-11 16:54 Nick Warne
  2005-06-11 17:06 ` Jan Engelhardt
  0 siblings, 1 reply; 12+ messages in thread
From: Nick Warne @ 2005-06-11 16:54 UTC (permalink / raw)
  To: linux-kernel

Robert Hancock wrote:

> Nick Warne wrote:
>> 2.4.31, GCC 3.4.4
>> 
>> Build like:
>> 
>> gcc -D__KERNEL__ -I/usr/src/linux/include -DMODULE -Wall -O2 -c hello.c
>> -o hello.o
> 
> That compilation method will not work on 2.6. You have to use the kernel
> makefiles to build the module. See:
> 
> http://linuxdevices.com/articles/AT4389927951.html

I see.  Sorry for the bum steer; I haven't messed about with 2.6.x much in 
development - I am still [trying to learn] learning 2.4.x.

How do you guys keep up with it all?

Great link, thanks.

Nick
-- 
"When you're chewing on life's gristle,
Don't grumble, Give a whistle..."

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
  2005-06-11 16:54 Nick Warne
@ 2005-06-11 17:06 ` Jan Engelhardt
  0 siblings, 0 replies; 12+ messages in thread
From: Jan Engelhardt @ 2005-06-11 17:06 UTC (permalink / raw)
  To: Nick Warne; +Cc: linux-kernel


>I see.  Sorry for the bum steer; I haven't messed about with 2.6.x much in 
>development - I am still [trying to learn] learning 2.4.x.
>
>How do you guys keep up with it all?

Either you have the "Wrote The Book" or better capability (sourceforge.net 
term), or you start all over from scratch - dismiss 2.4 and start with 2.6.



Jan Engelhardt                                                               
--                                                                            
| Gesellschaft fuer Wissenschaftliche Datenverarbeitung Goettingen,
| Am Fassberg, 37077 Goettingen, www.gwdg.de

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
  2005-06-11 12:11 Ilan S.
@ 2005-06-11 18:17 ` Sam Ravnborg
  2005-06-11 20:25   ` Jan Engelhardt
  2005-06-11 19:43 ` Jesper Juhl
  2005-06-13 20:09 ` Edward Macfarlane Smith
  2 siblings, 1 reply; 12+ messages in thread
From: Sam Ravnborg @ 2005-06-11 18:17 UTC (permalink / raw)
  To: Ilan S.; +Cc: linux-kernel

On Sat, Jun 11, 2005 at 03:11:02PM +0300, Ilan S. wrote:
> Hello dear professionals!
> 
> I would be very thankful if anybody prompt me what's wrong.
> I'm trying to build the "Hello world" module from O'Reilly's "Linux device 
> drivers" and that is what I get:
> 
> [ilanso@Netvision Kernel]$ make -C /home/ilanso/src/linux-2.6.11.11 M=`pwd`
> make: Entering directory `/home/ilanso/src/linux-2.6.11.11'
>   Building modules, stage 2.
>   MODPOST
> make: Leaving directory `/home/ilanso/src/linux-2.6.11.11'
> [ilanso@Netvision Kernel]$ ls
> hello.c  Makefile
> [ilanso@Netvision Kernel]$  

Can you please post your Makefile - seems something is wrong here.
You could also try to post the output with the V=1 flagg added when
invoking make.

	Sam

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
  2005-06-11 12:11 Ilan S.
  2005-06-11 18:17 ` Sam Ravnborg
@ 2005-06-11 19:43 ` Jesper Juhl
  2005-06-13 20:09 ` Edward Macfarlane Smith
  2 siblings, 0 replies; 12+ messages in thread
From: Jesper Juhl @ 2005-06-11 19:43 UTC (permalink / raw)
  To: Ilan S.; +Cc: linux-kernel

On 6/11/05, Ilan S. <ilan_sk@netvision.net.il> wrote:
> Hello dear professionals!
> 
> I would be very thankful if anybody prompt me what's wrong.
> I'm trying to build the "Hello world" module from O'Reilly's "Linux device
> drivers" and that is what I get:
> 
I don't have that book, so I can't really say, but here's an example
from "Linux Kernel Development, 2ed" by rml
(http://rlove.org/kernel_book/)


/*
 * hello.c - Hello, World! As a Kernel Module
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

/*
 * hello_init - the init function, called when the module is loaded.
 * Returns zero if successfully loaded, nonzero otherwise.
 */
static int hello_init(void)
{
        printk(KERN_ALERT "I bear a charmed life.\n");
        return 0;
}

/*
 * hello_exit - the exit function, called when the module is removed.
 */
static void hello_exit(void)
{
        printk("KERN_ALERT "out, out, brief candle!\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shakespeare");


To build the module above, make a Makefile in the same dir with this
line in it :

obj-m := hello.o


Then build like this:

make -C /kernel/source/location SUBDIRS=$PWD modules


Works for me :)

Of course there's lots of additional info in the book. Robert wrote a
rather good one, it's well worth checking out if you ask me.


-- 
Jesper Juhl <jesper.juhl@gmail.com>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
  2005-06-11 18:17 ` Sam Ravnborg
@ 2005-06-11 20:25   ` Jan Engelhardt
  2005-06-12 19:17     ` Sam Ravnborg
  0 siblings, 1 reply; 12+ messages in thread
From: Jan Engelhardt @ 2005-06-11 20:25 UTC (permalink / raw)
  To: Sam Ravnborg; +Cc: Ilan S., linux-kernel


>> [ilanso@Netvision Kernel]$ make -C /home/ilanso/src/linux-2.6.11.11 M=`pwd`

Try adding "modules" at the end?


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
  2005-06-11 20:25   ` Jan Engelhardt
@ 2005-06-12 19:17     ` Sam Ravnborg
  0 siblings, 0 replies; 12+ messages in thread
From: Sam Ravnborg @ 2005-06-12 19:17 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: Ilan S., linux-kernel

On Sat, Jun 11, 2005 at 10:25:30PM +0200, Jan Engelhardt wrote:
> 
> >> [ilanso@Netvision Kernel]$ make -C /home/ilanso/src/linux-2.6.11.11 M=`pwd`
> 
> Try adding "modules" at the end?

In 2.6 thats implicit when using M=... syntax.

	Sam

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
@ 2005-06-13 17:50 Nick Warne
  0 siblings, 0 replies; 12+ messages in thread
From: Nick Warne @ 2005-06-13 17:50 UTC (permalink / raw)
  To: linux-kernel

Jesper Juhl wrote:

> I don't have that book, so I can't really say, but here's an example
> from "Linux Kernel Development, 2ed" by rml
> (http://rlove.org/kernel_book/)

I bought this today!  If you are in UK, here is the best deal I could find - 
£22.00 + free delivery - I ordered this morning, it is dispatched already:

http://www.compman.co.uk/scripts/browse.asp?ref=706766

Why US on-line books are at least half the price of UK ones, I don't know., 
but of course then shipping costs/time buggers that up.
Rip-off UK, I suppose.

Thanks for the tip - I have been after a kernel book for ages, but wasn't sure 
what to get.

Nick
-- 
"When you're chewing on life's gristle,
Don't grumble, Give a whistle..."

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: 'hello world' module
  2005-06-11 12:11 Ilan S.
  2005-06-11 18:17 ` Sam Ravnborg
  2005-06-11 19:43 ` Jesper Juhl
@ 2005-06-13 20:09 ` Edward Macfarlane Smith
  2 siblings, 0 replies; 12+ messages in thread
From: Edward Macfarlane Smith @ 2005-06-13 20:09 UTC (permalink / raw)
  To: Ilan S.; +Cc: linux-kernel

On Saturday 11 June 2005 13:11, Ilan S. wrote:
> Hello dear professionals!
>
> I would be very thankful if anybody prompt me what's wrong.
> I'm trying to build the "Hello world" module from O'Reilly's "Linux device
> drivers" and that is what I get:
>
> [ilanso@Netvision Kernel]$ make -C /home/ilanso/src/linux-2.6.11.11 M=`pwd`
> make: Entering directory `/home/ilanso/src/linux-2.6.11.11'
>   Building modules, stage 2.
>   MODPOST
> make: Leaving directory `/home/ilanso/src/linux-2.6.11.11'
> [ilanso@Netvision Kernel]$ ls
> hello.c  Makefile
> [ilanso@Netvision Kernel]$
>

Hi Ilan,
I've been working through this book too, not sure why it's not working for 
you. Have you actually built the kernel in the directory you're pointing it 
at? I was able to use the command:
make -C /usr/src/linux M=`pwd`
where my Makefile had
        obj-m := eaglenet.o
The makefile I've generally used that works fine (so you can just type make) 
was:
ifneq ($(KERNELRELEASE),)
        obj-m := eaglenet.o
else
        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
        PWD := $(shell pwd)

default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif
Where my module was eaglenet.
Regards,
Edward

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2005-06-13 20:10 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <4egz4-2tj-15@gated-at.bofh.it>
2005-06-11 16:25 ` 'hello world' module Robert Hancock
2005-06-11 16:31   ` Jan Engelhardt
2005-06-13 17:50 Nick Warne
  -- strict thread matches above, loose matches on Subject: below --
2005-06-11 16:54 Nick Warne
2005-06-11 17:06 ` Jan Engelhardt
2005-06-11 15:53 Nick Warne
2005-06-11 12:11 Ilan S.
2005-06-11 18:17 ` Sam Ravnborg
2005-06-11 20:25   ` Jan Engelhardt
2005-06-12 19:17     ` Sam Ravnborg
2005-06-11 19:43 ` Jesper Juhl
2005-06-13 20:09 ` Edward Macfarlane Smith

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox