driver newbie

driver newbie

Post by in » Tue, 24 Jun 2003 16:22:36



I'm new in Linux programming. I'm trying out a simple  kernel module
program i've searched in the web. I've named the program "khello.c"

/-----------------------------------------------------------------------
#define MODVERSIONS
#include <linux/modversions.h>
#endif

/* Initialize the module */
int init_module()
{
  printk("Hello, world - this is the kernel speaking\n");

  /* If we return a non zero value, it means that
   * init_module failed and the kernel module
   * can't be loaded */
  return 0;

Quote:}

/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
  printk("Short is the life of a kernel module\n");
Quote:}

------------------------------------------------------------------------/

I use the following makefile to compile the program:

CC=gcc
MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX

khello.o: khello.c /usr/include/linux/version.h
        $(CC) $(MODCFLAGS) -c khello.c

-----------------------------------------------------------------------/
However, I get the following errors:

In file included from khello.c:5:
/usr/include/linux/module.h:299: parse error before "UTS_RELEASE"
/usr/include/linux/module.h: In function `print_symbol':
/usr/include/linux/module.h:433: `ESRCH' undeclared (first use in this
function)
/usr/include/linux/module.h:433: (Each undeclared identifier is
reported only once
/usr/include/linux/module.h:433: for each function it appears in.)

 
 
 

driver newbie

Post by Kasper Dupon » Tue, 24 Jun 2003 17:19:54



> I'm new in Linux programming. I'm trying out a simple  kernel module
> program i've searched in the web. I've named the program "khello.c"

> /-----------------------------------------------------------------------
> #define MODVERSIONS

That define is a litle spurious.

Quote:> #include <linux/modversions.h>

You are probably missing some includes.

Quote:> #endif

You shouldn't have an endif without a matching if.

Quote:

> /* Initialize the module */
> int init_module()
> {
>   printk("Hello, world - this is the kernel speaking\n");

>   /* If we return a non zero value, it means that
>    * init_module failed and the kernel module
>    * can't be loaded */
>   return 0;
> }

> /* Cleanup - undid whatever init_module did */
> void cleanup_module()
> {
>   printk("Short is the life of a kernel module\n");
> }

That is not how init and cleanup functions should be done
anymore. Take a look on a module in a recent kernel tree
to see how it is done now. (fs/ramfs/inode.c is a quite
simple examle, just look on the last page of the source.)

Quote:> ------------------------------------------------------------------------/

> I use the following makefile to compile the program:

> CC=gcc
> MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX

> khello.o: khello.c /usr/include/linux/version.h

The path in your dependency is wrong.

Quote:>         $(CC) $(MODCFLAGS) -c khello.c

You need a -I option somewhere. Take a look here:
http://www.kernelnewbies.org/faq/index.php3#compmod

Quote:

> -----------------------------------------------------------------------/
> However, I get the following errors:

> In file included from khello.c:5:
> /usr/include/linux/module.h:299: parse error before "UTS_RELEASE"
> /usr/include/linux/module.h: In function `print_symbol':
> /usr/include/linux/module.h:433: `ESRCH' undeclared (first use in this
> function)
> /usr/include/linux/module.h:433: (Each undeclared identifier is
> reported only once
> /usr/include/linux/module.h:433: for each function it appears in.)

--
Kasper Dupont -- der bruger for meget tid p? usenet.

It is NOT portable (Linus Benedict Torvalds 1991)

 
 
 

driver newbie

Post by Leigh W3NL » Tue, 24 Jun 2003 23:56:13


On Mon, 23 Jun 2003 10:19:54 +0200, Kasper Dupont



>> I'm new in Linux programming. I'm trying out a simple  kernel module
>> program i've searched in the web. I've named the program "khello.c"

>> /-----------------------------------------------------------------------
>> #define MODVERSIONS

>That define is a litle spurious.

>> #include <linux/modversions.h>

>You are probably missing some includes.

>> #endif

>You shouldn't have an endif without a matching if.

>> /* Initialize the module */
>> int init_module()
>> {
>>   printk("Hello, world - this is the kernel speaking\n");

>>   /* If we return a non zero value, it means that
>>    * init_module failed and the kernel module
>>    * can't be loaded */
>>   return 0;
>> }

>> /* Cleanup - undid whatever init_module did */
>> void cleanup_module()
>> {
>>   printk("Short is the life of a kernel module\n");
>> }

>That is not how init and cleanup functions should be done
>anymore. Take a look on a module in a recent kernel tree
>to see how it is done now. (fs/ramfs/inode.c is a quite
>simple examle, just look on the last page of the source.)

The test program that he's using is straight out of the book "Linux
Device Drivers, 2nd ed" by Rubini.

Best,
Leigh Bassett
Software architect and embedded systems guru.
Registered Linux user #307936
------------
If stupidity were a capital offense, I could enumerate the population
of the world on one finger... mine.