LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-28-2010, 06:51 PM   #1
kaching
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: Disabled
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!
 
Old 05-28-2010, 07:55 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by kaching View Post
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
 
1 members found this post helpful.
Old 06-07-2010, 12:44 PM   #3
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 30
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.
 
Old 06-07-2010, 01:25 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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!
 
Old 06-08-2010, 08:12 AM   #5
kaching
LQ Newbie
 
Registered: May 2010
Posts: 2

Original Poster
Rep: Reputation: Disabled
@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!
 
  


Reply

Tags
kernel, make, module, ubuntu


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial for writing cypher kernel module ta0kira Programming 7 06-19-2011 11:43 PM
linux kernel module re-writing bony Programming 1 03-20-2010 12:10 PM
Kernel Module Writing itz2000 Programming 6 10-05-2007 04:54 PM
need help in writing Makefile for module in 2.6 kernel videsh Programming 1 01-19-2006 08:35 AM
need help in writing Makefile for module in 2.6 kernel videsh Linux - Software 3 01-19-2006 08:30 AM


All times are GMT -5. The time now is 07:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration