LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   writing a trivial kernel module - help (http://www.linuxquestions.org/questions/programming-9/writing-a-trivial-kernel-module-help-810822/)

kaching 05-28-2010 06:51 PM

writing a trivial kernel module - help
 
Hi, I am trying to learn how to write a kernel module.
I am following the excellent guide from The Linux Documentation Project called The Linux Kernel Module Programming Guide v.2.6.4.

My machine is running Ubuntu Lucid Lynx (10.04)
Code:

uname -r
2.6.32-22-generic

I installed the corresponding linux headers and just to make sure I also installed the linux source and extracted it in /usr/src

I am trying to run the following trivial kernel module
Code:

/* 
 *  hello-1.c - The simplest kernel module.
 */
#include <linux/module.h>        /* Needed by all modules */
#include <linux/kernel.h>        /* Needed for KERN_INFO */

int init_module(void)
{
        printk(KERN_INFO "Hello world 1.\n");

        /*
        * A non 0 return means init_module failed; module can't be loaded.
        */
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_ALERT "Goodbye world 1.\n");
}

with the following Makefile
Code:

obj−m += hello−1.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

as it appears in pages 5 and 6 of the pdf version of the tutorial above.
I got the following error from make
Code:

uname: extra operand `−r'
Try `uname --help' for more information.
make −C /lib/modules//build M=/home/sonny/km modules
make[1]: Entering directory `/home/sonny/km'
make[1]: *** No rule to make target `−C'.  Stop.
make[1]: Leaving directory `/home/sonny/km'
make: *** [all] Error 2

So, from the first and third lines I saw there was a problem with uname -r
so I changed the Makefile to
Code:

obj−m += hello-1.o
KVERSION = $(shell uname -r)
all:
        make −C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make −C /lib/modules/$(KVERSION)/build M=$(PWD) clean

and then running make corrected the first error, but I still get the following:
Code:

make −C /lib/modules/2.6.32-22-generic/build M=/home/sonny/km modules
make[1]: Entering directory `/home/sonny/km'
make[1]: *** No rule to make target `−C'.  Stop.
make[1]: Leaving directory `/home/sonny/km'
make: *** [all] Error 2

What am I doing wrong? All the headers are in place and the source is there as well.
lib/modules/2.6.32-22-generic links to (what I think are) the right places.
Any help would be much appreciated.

Thanks a lot!

JohnGraham 05-28-2010 07:55 PM

Quote:

Originally Posted by kaching (Post 3984762)
Code:

uname -r
2.6.32-22-generic

Code:

obj−m += hello−1.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

Code:

make C /lib/modules/2.6.32-22-generic/build M=/home/sonny/km modules
make[1]: Entering directory `/home/sonny/km'
make[1]: *** No rule to make target `−C'.  Stop.
make[1]: Leaving directory `/home/sonny/km'
make: *** [all] Error 2


It would appear the some of the characters you intend to be '-'-characters (hexadecimal value 0x2a) are in fact unicode '−' characters (0xe28892) - if it helps, I see the former kind as slightly shorter on my web browser.

Either way, that'll be why it's not recognising "−C" as an option - you need to make sure it's "-C".

John G

ArthurSittler 06-07-2010 12:44 PM

make is not magical
 
kaching, I had not looked at this issue closely until now.

I believe the problem may be a confusion about what make is and what it does. Make is a scripting language for automating the process of software development or any other process which requires some sequence of one or more commands to the command shell. I use it for compiler writing and other software generation using flex and bison because it automates calling flex and bison, followed by compiling and linking the programs written by flex and bison. You don't need to use make to compile and link kernel modules. If you have not written a makefile before, it may be easier if you become familiar with using make independently of learning how to build kernel modules. Please refer to the man page about make and perhaps other material to get a solid feel for using make before trying to use a makefile to build your kernel module. You can do this either before or after you build your kernel module, but doing both at the same time adds an extra layer of difficulty to your ostensible goal of building the module.

I will follow up on this later, I need to go to another duty right now.

paulsm4 06-07-2010 01:25 PM

Hi, JohnGraham -

Good eyes!

The OP hasn't posted back, but I believe you found the problem.

Specifically, "uname -r" and "make -C" need a MINUS SIGN. For us 7-bit ASCII guys, that means exactly one character: 0x2d (ASCII 45).

But in Unicode, it can mean any of "hyphen-minus" (the character we need), hyphen, figure-dash, em-dash, etc etc (one of which I believe the OP accidentally used).

Anything besides "hyphen-minus" (0x2d) will result in exactly the errors shown above.

Here's a link:
http://www.decodeunicode.org/en/u+2212/properties

kaching -

Please post back what you found. And please mark the problem "solved", if you fixed it!

kaching 06-08-2010 08:12 AM

@JohnGraham, thanks a lot you were right on the money! You indeed solved my problem (and to the rest following this thread, I apologize for not updating the thread as solved before.
@ArthurSittler thanks for the advice with make. Although I think that the Makefile in this case was easy enough for a novice programmer as myself to follow.

I am still hitting other walls writing this kernel module, but that's for another thread...
At least I got it to compile...

Thanks again!
[SOLVED] - Yippee!


All times are GMT -5. The time now is 11:55 PM.