Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-13-2011, 06:18 AM   #1
LQ Newbie
Registered: Nov 2007
Posts: 20

Rep: Reputation: 0
Writing a kernel module and make concepts

i have written some really basic loadable kernel modules. There are a couple of concepts that i'll like to get cleared.

In a tutorial, the Makefile was given as follows-
obj-m += try.o

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

I understand that -C option of 'make' changes the current directory to /lib/modules/$(shell uname -r)/build (which depends on my kernel).

Now, my question is why to change to that particular directory? Also
M=$(PWD) will hold the dir where i have written the module. Why is it required? What does 'modules' at the end of that line indicate?

Q2> We need to include <linux/module.h> and <linux/kernel.h> in a module. If i am right, the module uses kernel headers for these definitions. But how does 'make' find these definitions.

Q3> We are planning to write a custom scheduler for linux kernel.
cat /sys/block/sda/queue/scheduler gives us the currently loaded schedulers. How do we expose our scheduler to linux kernel so that we can use it by switching to it.
Old 03-13-2011, 09:01 AM   #2
LQ Newbie
Registered: Nov 2007
Posts: 20

Original Poster
Rep: Reputation: 0
Is this question in wrong section??
Old 03-13-2011, 01:05 PM   #3
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
make works only on Makefiles in the current working directory. The '-C' option tells make to first go to the specified directory, and use the specified target in the Makefile in that directory. This give make the capability of recursive building. The 'target' is one of the tokens at the left margin and followed by a ':' in a Makefile. The tokens 'modules' and 'clean' are such targets. Specifying a certain target tells make what part of the Makefile recipe it supposed to follow.
Make doesn't know how to find any C header files. Only C compilers know how to find header files. You may write rules in a Makefile that add to the C compilers list of places to look for header files.
To see how to add your scheduler, look through the Makefiles for an existing kernel, and see how the existing schedulers are enumerated. Add yours to the Makefile using a similar format. It may also require adding accordant entries to the kernel .config file.

A good understanding of the make tool is a valuable asset as a software developer. As a kernel developer, it is probably essential. I recommend finding one or more tutorials online to help you. Also, the man page for make has useful information.

--- rod.
Old 03-13-2011, 03:23 PM   #4
LQ 5k Club
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2016, Slack14.1 Deb Jessie, + 50+ other Linux OS, for test only.
Posts: 16,276

Rep: Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156
... Another example ..
obj-m := lpptest.o
KDIR    := /lib/modules/$(shell uname -r)/build
PWD    := $(shell pwd)

        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules


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
[SOLVED] writing a trivial kernel module - help kaching Programming 4 06-08-2010 08:12 AM
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 Linux - Software 3 01-19-2006 08:30 AM
package compiling from source, make & make install concepts shujja Linux - Newbie 2 09-20-2005 01:18 AM

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

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