compiling a new module

304 views
Skip to first unread message

vivek-MT2009146

unread,
Feb 18, 2010, 10:30:08 PM2/18/10
to Linux kernel IIITB
I am getting following error while compiling a linix kernel module for
hello world.

cc -c hello-1.c
hello-1.c:1:25: error: linux/module.h: No such file or directory
hello-1.c: In function ‘init_module’:
hello-1.c:6: error: ‘KERN_INFO’ undeclared (first use in this
function)
hello-1.c:6: error: (Each undeclared identifier is reported only once
hello-1.c:6: error: for each function it appears in.)
hello-1.c:6: error: expected ‘)’ before string constant
hello-1.c: In function ‘cleanup_module’:
hello-1.c:14: error: ‘KERN_INFO’ undeclared (first use in this
function)
hello-1.c:14: error: expected ‘)’ before string constant
make: *** [hello.o] Error 1

Help....

vivek-MT2009146

unread,
Feb 18, 2010, 10:31:17 PM2/18/10
to Linux kernel IIITB
Program:

#include<linux/module.h>
#include<linux/kernel.h>

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

return 0;

}

void cleanup_module(void)
{
printk(KERN_INFO "GOOD BYE\n");
}

Kapil Ratnani

unread,
Feb 18, 2010, 10:54:36 PM2/18/10
to kernel...@googlegroups.com
just install linux-kernel-headers package.. that will fix it..
--
Kapil Ratnani
MT2009119
================================
                       TheRiPPer
================================

VivekKumar Singh

unread,
Feb 18, 2010, 10:59:12 PM2/18/10
to kernel...@googlegroups.com
Hi Kapil,

I tried to install, but it says, it is already installed there.


sudo apt-get install linux-headers-$(uname -r)
Reading package lists... Done
Building dependency tree      
Reading state information... Done
linux-headers-2.6.28-17-
generic is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 66 not upgraded
--
Thanks and Regards,

Vivek Kumar Singh
MT-2009146
IIITB, Bangalore

(91)9886317184

Kapil Ratnani

unread,
Feb 18, 2010, 11:42:43 PM2/18/10
to kernel...@googlegroups.com
hey run this command
dpkg -l linux*

This will list all the packages installed with name starting from "linux". post the output here..

VivekKumar Singh

unread,
Feb 19, 2010, 12:00:28 AM2/19/10
to kernel...@googlegroups.com
dpkg -l linux*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name           Version        Description
+++-==============-==============-============================================
un  linux-boot-loa <none>         (no description available)
un  linux-doc-2.6. <none>         (no description available)
ii  linux-firmware 1.11           Firmware for Linux kernel drivers
ii  linux-generic  2.6.28.17.22   Complete Generic Linux kernel
un  linux-headers  <none>         (no description available)
un  linux-headers- <none>         (no description available)
pn  linux-headers- <none>         (no description available)
pn  linux-headers- <none>         (no description available)
ii  linux-headers- 2.6.28-14.47   Header files related to Linux kernel version
ii  linux-headers- 2.6.28-14.47   Linux kernel headers for version 2.6.28 on x
ii  linux-headers- 2.6.28-15.52   Header files related to Linux kernel version
ii  linux-headers- 2.6.28-15.52   Linux kernel headers for version 2.6.28 on x
ii  linux-headers- 2.6.28-17.58   Header files related to Linux kernel version
ii  linux-headers- 2.6.28-17.58   Linux kernel headers for version 2.6.28 on x
ii  linux-headers- 2.6.28.17.22   Generic Linux kernel headers
un  linux-image    <none>         (no description available)
un  linux-image-2. <none>         (no description available)
ii  linux-image-2. 2.6.28-11.42   Linux kernel image for version 2.6.28 on x86
ii  linux-image-2. 2.6.28-14.47   Linux kernel image for version 2.6.28 on x86
ii  linux-image-2. 2.6.28-15.52   Linux kernel image for version 2.6.28 on x86
ii  linux-image-2. 2.6.28-17.58   Linux kernel image for version 2.6.28 on x86
ii  linux-image-ge 2.6.28.17.22   Generic Linux kernel image
un  linux-initramf <none>         (no description available)
un  linux-kernel-h <none>         (no description available)
un  linux-kernel-l <none>         (no description available)
ii  linux-libc-dev 2.6.28-17.58   Linux Kernel Headers for development
un  linux-restrict <none>         (no description available)
ii  linux-restrict 2.6.28-11.15   Non-free Linux kernel modules for version 2.
ii  linux-restrict 2.6.28-14.19   Non-free Linux kernel modules for version 2.
ii  linux-restrict 2.6.28-15.20   Non-free Linux kernel modules for version 2.
ii  linux-restrict 2.6.28-17.22   Non-free Linux kernel modules for version 2.
ii  linux-restrict 2.6.28-17.22   Non-free Linux 2.6.28 modules helper script
ii  linux-restrict 2.6.28.17.22   Restricted Linux modules for generic kernels
ii  linux-sound-ba 1.0.18.dfsg-1u base package for ALSA and OSS sound systems
un  linux-source-2 <none>         (no description available)
un  linux32        <none>         (no description available)
un  linuxdoc-tools <none>         (no description available)
un  linuxprinting. <none>         (no description available)
vivek@iiitb-laptop1:~/kernel-modules$

VivekKumar Singh

unread,
Feb 19, 2010, 12:04:04 AM2/19/10
to kernel...@googlegroups.com
wiki page for linux kernel:

share your work here:

http://wiki.iiitb.ac.in/mediawiki/index.php/LINUX_KERNEL

Kapil Ratnani

unread,
Feb 19, 2010, 12:15:13 AM2/19/10
to kernel...@googlegroups.com
hey, this is not the way to compile a kernel module...
kernel modules are compiled using kbuild. use this makefile to compile:
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

actually using your case you first have to change the directory to lib/modules/linux<ver>/
to compile the module followed by other additional steps to get the final kernel module .ko file,
use this automated method for convenience
Reply all
Reply to author
Forward
0 new messages