Professional Documents
Culture Documents
html
About
Forum
Howtos & FAQs
Low graphics
Shell Scripts
RSS/Feed
This is one the essential and important task. Many time we upgrade our kernel and some
precompiled drivers won't work with Linux. Especially if you have weird hardware; then
vendor may send you driver code aka C files to compile. Or even you can write your own
Linux kernel driver. Compiling kernel driver is easy. Kernel 2.6.xx makes it even much
more easier. Following steps are required to compile driver as module:
1) You need running kernel source code; if you don't have a source code download it from
kernel.org. Untar kernel source code (tar ball) in /usr/src using tar command:
$ tar -zxvf kernel* -C /usr/src
To be frank kernel headers are more than sufficient to compile kernel modules / drivers. See how to install
kernel headers under Debian / Ubuntu Linux or RHEL / CentOS / Fedora Linux.
2) Next go to your kernel module source code directory and simply create the Makefile file as follows (assuming
your kernel module name is foo):
$ vi Makefile
obj-m = foo.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
4) Compile module using make command (module build can be done by any user) :
$ make
It will finally creates the foo.ko module in current directory. You can see all actual compile command stored in
.foo* files in same directory.
5) Once module compiled successfully, load it using insmod or modprobe command. You need to be root user or
1 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
obj-m = hello.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
Note you can see message on screen if you are logged in as root under run level 3.
2 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
# tail -f /var/log/message
10) Load module when Linux system comes up. File /etc/modules use to load kernel boot time. This file should
contain the names of kernel modules that are to be loaded at boot time, one per line. First copy your module to
/lib/modules/$(uname -r)/kernel/drivers. Following are suggested steps:
See also:
Read man pages of lsmod, rmmod, modprobe, modules
Documentation located in your kernel source directory (for example /usr/src/linux-2.6.xx.xx
/Documentation/) and README file located under kernel source code tree /usr/src/linux-2.6.xx.xx
/README
Read TLDP.org tutorial online.
Featured Articles:
We're here to help you make the most of sysadmin work. So, subscribe!
3 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Reply
hi when i compile this by running make file this type of erro occcur …what to do
make -f make
make -C /lib/modules/2.6.18-1.2798.fc6/build M=/home/mclamna modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-1.2798.fc6-i686′
scripts/Makefile.build:17: /home/mclamna/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/mclamna/Makefile’. Stop.
make[1]: *** [_module_/home/mclamna] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-1.2798.fc6-i686′
make: *** [all] Error 2
Reply
CHK include/linux/version.h
CHK include/linux/utsrelease.h
HOSTCC scripts/basic/fixdep
In file included from /usr/include/bits/posix1_lim.h:153,
from /usr/include/limits.h:145,
from /usr/lib/gcc/i486-linux-gnu/4.1.2/include/limits.h:122,
from /usr/lib/gcc/i486-linux-gnu/4.1.2/include/syslimits.h:7,
from /usr/lib/gcc/i486-linux-gnu/4.1.2/include/limits.h:11,
from scripts/basic/fixdep.c:113:
/usr/include/bits/local_lim.h:36:26: error: linux/limits.h: No such file or directory
In file included from /usr/include/sys/socket.h:35,
from /usr/include/netinet/in.h:24,
from /usr/include/arpa/inet.h:23,
from scripts/basic/fixdep.c:115:
/usr/include/bits/socket.h:310:24: error: asm/socket.h: No such file or directory
scripts/basic/fixdep.c: In function ‘use_config’:
scripts/basic/fixdep.c:204: error: ‘PATH_MAX’ undeclared (first use in this function)
scripts/basic/fixdep.c:204: error: (Each undeclared identifier is reported only once
scripts/basic/fixdep.c:204: error: for each function it appears in.)
scripts/basic/fixdep.c:204: warning: unused variable ‘s’
scripts/basic/fixdep.c: In function ‘parse_dep_file’:
scripts/basic/fixdep.c:300: error: ‘PATH_MAX’ undeclared (first use in this function)
scripts/basic/fixdep.c:300: warning: unused variable ‘s’
make[1]: *** [scripts/basic/fixdep] Error 1
make: *** [scripts_basic] Error 2
4 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Reply
Hi swift, I guess u have not created Makefile in ur present directory, plz make sure that makefile name is
correct and its case sensitive, it should be ‘Makefile’.
Reply
Hi, This was very much helpful but now I’m stuck because i didn’t understand how this particular
makefile is working, so i’m not able to write it for multiple files. Please could anyone explain how this
makefile works with a example for multiple files.
Thanks,
Nitin.
Reply
Reply
Dear Vivek ,
Thanks alots for yr teaching.
i compile a new module on fedora core 6 and make it very good. but in fedora i dont know how to submit
for when linux start up , can load automaticly. i didnot find /etc/modules in fedora.
Reply
I followed the instructions as written even copying and pasting the code, I got nada.
then I copied and pasted the hello.c text and the Makefile from this page but I got nothing.
make: Nothing to be done for ‘all’.
Reply
ok I got it somehow, honestly I am not sure how but I patched my install (now running kernel-headers
5 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
2.6.24-21 generic) and rebooted, sudo -i, just tried running make and it worked.
before I was getting “can’t find target ‘make’ error that seems so common. It now is working!!!
Reply
Reply
The reason that make: Nothing to be done for ‘all’ is you should modify the Makefile, replace the space in
each make command with tab.
Reply
i am getting the same error and replacing spaces with tab is not working.
some other solution..??
Reply
After executing the insmod the message is not being displayed…Can u pls guide me on whats goin
wrong?
Reply
Reply
Reply
Hi,
I’m trying to compile your hello.c module in a Fedora Core 9 with a 2.6.27.21-78.2.41.fc9.i686 kernel.
My gcc compiler is 4.3.0 20080428 (Red Hat 4.3.0-8) (GCC). My kernel-devel package is installed with
6 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
root@leela:[~/pruebas-modulos]$ make
make -C /lib/modules/2.6.27.21-78.2.41.fc9.i686/build M=/root/pruebas-modulos modules
make[1]: se ingresa al directorio `/usr/src/kernels/2.6.27.21-78.2.41.fc9.i686′
Building modules, stage 2.
MODPOST 0 modules
make[1]: se sale del directorio `/usr/src/kernels/2.6.27.21-78.2.41.fc9.i686′
root@leela:[~/pruebas-modulos]$ ls
hello.c hello.o Makefile Module.markers modules.order Module.symvers
root@leela:[~/pruebas-modulos]$
As you can see I do not obtain the .ko file. I have searched across the web in order to understand why I am
not getting my .ko file, but I am not able to find anything useful. I would appreciate if you could help me
to find why the makefile is not working
Thanks in advance,
Víctor.
Reply
Hey, check that you makefile if you make a mistake with “obj-m” with “obj_m”, I have the same problem
as you do.
When I write “obj-m” instead of “obj_m”, everything goes good.
Reply
Be sure you have tabs instead of spaces in the Makefile. Obvious and common mistake but took me a few
minutes to figure this out after copy-pasting.
Reply
hi,
could u please tell me how can i install the kernel updates as I despirately need linux source code.
Reply
Thank you for posting this. It is very hard to figure out how to just make a single module manually. This
post should be ranked higher when searching “How to compile a single kernel module”. Self fulfilling
prophecy, perhaps?
7 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Reply
Reply
Hi
I have tried load module when Linux system comes up but it’s not displaying in
#cat /proc/modules
I have done these things successfully.
# mkdir -p /lib/modules/$(uname -r)/kernel/drivers/hello
# cp hello.ko /lib/modules/$(uname -r)/kernel/drivers/hello/
# vi /etc/modules
with regards
Alok
Reply
Reply
Hi,
I found the article very helpful. Unlike many other linux tutorials, this one worked (with me) within the
first few attempts! Just one minor point:
I had to change
tail -f /var/log/message
to
tail -f /var/log/messages
Arnab.
Reply
Reply
8 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Please can anybody help me out, I need to add the pktgen module, but I’m finding it difficult
Thanks in advance
Reply
hi
thanks for that article. I try to compile a helloworld.c against the sources of my running kernel now quite a
while without success. First the distro-kernel, and then also the kernel.org kernel. It all looks good to me.
make enters the right directories. the error is _always_ “No Rule for target ….. ” like in the first comment
above.
I don’t really understand all about the makefile. make is called from inside the kernel tree through the
makefile right? Still, it really should be fine, but it doesnt work. Does anybody have a hint?
Reply
Hi guys… I’m getting really desperate, I need to program a network device drivers for one of my projects,
and I can’t even get the hello world done properly !
I followed the protocol above, and when it comes to the make command, here is what I get:
root@ubuntu:/usr/src/linux-source-2.6.31/include# make
So no hello.ko file generated ! I already spent so many hours on this, checked that I have tabs instead of
spaces in the makefile, and nothing changed…
zed
Reply
Since PWD is not defined, I think it attempts to compile the entire kernel source.
It solved this error I got:
“make[2]: *** No rule to make target `kernel/bounds.c’, needed by `kernel/bounds.s’. Stop.”
9 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Reply
Thanks a lot to mix and reveil for pointing out the tabs in place of spaces. I spent like 3 hrs trying to build
a module from another site, and had no clue what was wrong. It all boiled down to tabs.
Also thanks to whoever made this tut. :)
Reply
One thing I learned from this exercise is to always start from a working example.
Reply
Checked it.
Following is my program
#include
#include
#include
extern void *sys_table[];
asmlinkage int(*main_sys_exit)(int);
asmlinkage int alt_exit_function(int err_code)
{
printk(“Sys_exit called with err_code=%d\n”,err_code);
return main_sys_exit(err_code);
}
int init_module()
{
main_sys_exit=sys_table[__NR_exit];
sys_table[__NR_exit]=alt_exit_function;
}
void cleanup_module()
{
sys_table[__NR_exit]=main_sys_exit;
}
10 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Reply
START WITH THE EXAMPLE, WHICH HAS BEEN DEBUGGED & TESTED FOR YOU.
If you cannot get that to work that is one thing.
If you can get THAT to work but you cannot get YOUR code to work then
for chrissakes
figure out what you have done different that doesn’t work and fix it l!!!!
Reply
hello,
my system is ubuntu 10.04 kernel 2.6.32-25-generic, i did everything as described in article and.. when i
do(as root: sudo -i ):
# insmod hello.ko
i get answer :
# insmod: error inserting ‘hello.ko’: -1 Invalid module format
And I’ve no idea what can be wrong.. or what I do wrong… would be gratefull for help
Reply
Cool! :-)
I compiled my first module in Linux.
Reply
I am getting this error from so long. I even changed my working directory to root.
11 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Reply
How did you fix this problem Prashanth? I’m getting exactly same error make[1]: *** No rule to
make target `−C’. Stop. Please help me if you have some info on this.
Reply
I have trouble with my custom kernel for the last tow days, it does not boot due [b]ahci[/b] module, can I
compile it as module and then make initrd image or I have to add it to kernel configuration, if so where I
have to activate it, I tried to go to:
Device drivers-> Serial ATA Drivers and Parelle ATA Drivers-> AHCI SATA Support
But it seems does not compile it according to this message when I install modules:
[code]
WARNING: No module ahci found for kernel 2.6.37-rc4, continuing anyway
[/code]
Reply
I have trouble with my custom kernel for the last tow days, it does not boot due ahci module, can I
compile it as module and then make initrd image or I have to add it to kernel configuration, if so where I
have to activate it, I tried to go to:
Device drivers-> Serial ATA Drivers and Parelle ATA Drivers-> AHCI SATA Support
But it seems does not compile it according to this message when I install modules:
Reply
Reply
Reply
The line:
obj-m = foo.o
12 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
obj-m += foo.o
Reply
Reply
Reply
um, obviously one part of the problem here is the fact that depending on what editor you use, what exactly
the offsets are between different sections of the relevant files and whether you use spaces or tabs, you will
get different results.
that’s on top of exactly what version of Linux you’re running, and what version of the relevant libraries.
Trust me on this. Start with a working example, one that you simply download and run, with no edits. If
you don’t have one find one on the internet somewhere. The most that you can pick up from a guide like
this are just guidelines for what should work. Doesn’t mean that it will work for you on your system, the
way that you’ve configured it or edited the file.
When it should work and it does not? odds are that you are about to learn something new about Linux.
good luck
Reply
Reply
If you’re really having trouble getting this to fly, then I would suggest reading this:
http://www.gnu.org/software/make/manual/make.html#Overview
The full documentation on gnu make
probably will help a lot.
and as always
start with something that works and make minimal changes between verifications
13 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
something as small as a simple tab where the make utility expects spaces or vice-versa
will definitely keep it from working. Not to mention an out-of-date install or an install that needs a reboot.
Reply
Reply
Thank you. ^^
– screen dump –
Module Size Used by
hello 786 0
powernow_k8 10978 1
cpufreq_stats 2740 0
cpufreq_conservative 5162 0
cpufreq_userspace 1992 0
parport_pc 18855 0
cpufreq_powersave 902 0
ppdev 5030 0
Reply
Hi,
when I try to do the same with the different code; getting the following error. Any Help is really
appreciated.
> make
make -C /lib/modules/2.6.25.5-1.1-default/build SUBDIRS=/home/vmware1/netp15 modules
make[1]: Entering directory `/usr/src/linux-2.6.25.5-1.1-obj/x86_64/default’
make[1]: *** No rule to make target `modules’. Stop.
make[1]: Leaving directory `/usr/src/linux-2.6.25.5-1.1-obj/x86_64/default’
make: *** [all] Error 2
-Thnx,
VKS.
Reply
Leave a Comment
14 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Name *
E-mail *
Website
You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul>
<blockquote> <pre> <a href="" title="">
Security Question:
What is 15 + 6 ?
Are you a human being? Solve the simple math so we know that you are a human and not a bot.
Tagged as: kernel module, kernel source code, linux kernel driver, make command, makefile, module source code, tar command, vi
command
15 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Find us on Facebook
nixCraft
Like
16 of 17 2/15/2012 6:16 PM
How to: Compile Linux kernel modules http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html
Related Posts
©2004-2012 nixCraft. All rights reserved. Cannot be reproduced without written permission.
Privacy Policy | Terms of Service | Questions or Comments | Copyright Info | Sitemap
17 of 17 2/15/2012 6:16 PM