LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-13-2011, 05:18 AM   #1
paresh.nakhe
LQ Newbie
 
Registered: Nov 2007
Posts: 20

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


Hi,
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

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

clean:
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, 08:01 AM   #2
paresh.nakhe
LQ Newbie
 
Registered: Nov 2007
Posts: 20

Original Poster
Rep: Reputation: 0
Is this question in wrong section??
 
Old 03-13-2011, 12:05 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
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, 02:23 PM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2016 CentOS6.9 CentOS7.3 + 50+ other Linux OS, for test only.
Posts: 16,635

Rep: Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268
... Another example ..
http://www.linuxquestions.org/questi...rnel-660415/#4
Code:
obj-m := lpptest.o
KDIR    := /lib/modules/$(shell uname -r)/build
PWD    := $(shell pwd)

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


Reply


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 07:12 AM
linux kernel module re-writing bony Programming 1 03-20-2010 11:10 AM
Kernel Module Writing itz2000 Programming 6 10-05-2007 03:54 PM
need help in writing Makefile for module in 2.6 kernel videsh Linux - Software 3 01-19-2006 07:30 AM
package compiling from source, make & make install concepts shujja Linux - Newbie 2 09-20-2005 12:18 AM


All times are GMT -5. The time now is 04:19 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration