We help IT Professionals succeed at work.

How to compile linux kernel module

jl66
jl66 asked
on
2,442 Views
Last Modified: 2013-12-06
Want to compile a linux kernel module. Start to work on it from a simple hello example, here is error info:
$ make hello
cc     hello.c   -o hello
hello.c:1:60: error: linux/module.h: No such file or directory
hello.c:3:60: error: linux/init.h: No such file or directory
hello.c:5: error: expected =, ,, ;, asm or __attribute__ before hello_start
hello.c:12: error: expected =, ,, ;, asm or __attribute__ before hello_end
hello.c:17: warning: data definition has no type or storage class
hello.c:17: warning: parameter names (without types) in function declaration
hello.c:18: warning: data definition has no type or storage class
hello.c:18: warning: parameter names (without types) in function declaration
make: *** [hello] Error 1
--------------------------------
What 's wrong with it? Looking forwards to hear from experts.
hello.c
#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/init.h>         /* Needed for the macros */
 
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
 
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
 
module_init(hello_start);
module_exit(hello_end);
 
Makefile:
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

Open in new window

Comment
Watch Question

Top Expert 2005

Commented:
Do You have installed "kernel-devel" package?
yum install kernel-devel

also, it may happen, that the running kernel and kernel-devel version differ - boot to proper kernel then, or change the
KVERSION = $(shell uname -r)
to
KVERSION = 2.6.27.19-170.2.35.fc10.i686 # change to kernel-devel package version
jl66Consultant

Author

Commented:
I installed the kernel-devel package, whose version is different from the fedora 10 binary version. Could you please elaborate your solution in detail?
In which file can I change the KVERSION?

Top Expert 2005

Commented:
> In which file can I change the KVERSION?
In the makefile You provided.
say
# rpm -q kernel-devel # says
kernel-devel-2.6.27.19-170.2.35.fc10.i686
then edit the Makefile and change the KVERSION. Of course module compiled for kernel other than running will not load to this kernel.
jl66Consultant

Author

Commented:
I copied the Makefile. Do you think it makes sense in Fedora 10? If not, do you have a good example for it?
jl66Consultant

Author

Commented:
It does not seem working:
$ cat Makefile
obj-m = hello.o
KVERSION = 2.6.27.19-170.2.35.fc10.i686
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
[jeff@localhost demo]$ make hello
cc     hello.c   -o hello
hello.c:1:60: error: linux/module.h: No such file or directory
hello.c:3:60: error: linux/init.h: No such file or directory
hello.c:5: error: expected =, ,, ;, asm or __attribute__ before hello_start
hello.c:12: error: expected =, ,, ;, asm or __attribute__ before hello_end
hello.c:17: warning: data definition has no type or storage class
hello.c:17: warning: parameter names (without types) in function declaration
hello.c:18: warning: data definition has no type or storage class
hello.c:18: warning: parameter names (without types) in function declaration
make: *** [hello] Error 1

What else can I try?
Top Expert 2005

Commented:
try issuing just
make
jl66Consultant

Author

Commented:
Still can't make it.
$ cat Makefile
obj-m = hello.o
KVERSION = 2.6.27.19-170.2.35.fc10.i686
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
[jeff@localhost demo]$ make
make: Nothing to be done for `all'.
Top Expert 2005

Commented:
I suppose You are lacking the <tab> char before make in the Makefile, use the attached one.

Makefile
jl66Consultant

Author

Commented:
I used your makefile to make hello module, but it still failed.
[jeff@localhost demo]$ make
make -C /lib/modules/2.6.27.19-170.2.35.fc10.i686/build M=/home/jeff/demo modules
make: *** /lib/modules/2.6.27.19-170.2.35.fc10.i686/build: No such file or directory.  Stop.
make: *** [all] Error 2

I checked I could not find the directory /lib/modules/2.6.27.19-170.2.35.fc10.i686. Should we have to make a link? BTW, did you test the Makefile in your system? How did it go?
Top Expert 2005
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
jl66Consultant

Author

Commented:
I downloaded the source code linux-2.6.29.tar.bz2. Could you point me to a link on how to load the new source in or list the steps I can follow for it? I found so many links, but lack knowledge on how to select it.
Top Expert 2005

Commented:
Why You downloaded those sources.
You better give me output from those two commands
rpm -qa | grep ^kernel
uname -r
jl66Consultant

Author

Commented:
Here they are:

[jeff@localhost ~]$ rpm -qa | grep ^kernel
kernel-2.6.27.5-117.fc10.i686
kernel-devel-2.6.27.19-170.2.35.fc10.i686
kerneloops-0.12-1.fc10.i386
kernel-firmware-2.6.27.5-117.fc10.noarch
kernel-headers-2.6.27.19-170.2.35.fc10.i386

[jeff@localhost ~]$ uname -r
2.6.27.5-117.fc10.i686
Top Expert 2005
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
jl66Consultant

Author

Commented:
After issuing that command and rebooting the server, re-issue the commands:

[jeff@localhost Documents]$ rpm -qa | grep ^kernel
kernel-2.6.27.5-117.fc10.i686
kernel-devel-2.6.27.19-170.2.35.fc10.i686
kerneloops-0.12-1.fc10.i386
kernel-devel-2.6.27.21-170.2.56.fc10.i686
kernel-headers-2.6.27.19-170.2.35.fc10.i386
kernel-2.6.27.21-170.2.56.fc10.i686
kernel-firmware-2.6.27.21-170.2.56.fc10.noarch

[jeff@localhost Documents]$ uname -r
2.6.27.21-170.2.56.fc10.i686
Top Expert 2005

Commented:
This is very strange, have You blocked the kernel updates in yum.conf?
Tyr installing: yum install kernel-2.6.27.19-170.2.35.fc10.i686
jl66Consultant

Author

Commented:
[root@localhost ~]# yum install kernel-2.6.27.19-170.2.35.fc10.i686
Loaded plugins: refresh-packagekit
updates                                                  | 2.3 kB     00:00    
fedora                                                   | 2.8 kB     00:00    
Setting up Install Process
Parsing package install arguments
No package kernel-2.6.27.19-170.2.35.fc10.i686 available.
Nothing to do
----------------------
How do you think?
jl66Consultant

Author

Commented:
I did not do anything on blocking the config. file. Can you tell me the commands I can check?
Top Expert 2005

Commented:
Sure, there's already newer one
Please download
ftp://download.fedora.redhat.com/pub/fedora/linux/updates/10/i386/kernel-2.6.27.21-170.2.56.fc10.i686.rpm
ftp://download.fedora.redhat.com/pub/fedora/linux/updates/10/i386/kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm
install them with
yum localinstall kernel-2.6.27.21-170.2.56.fc10.i686.rpm kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm
reboot
change the Makefile line back to
KVERSION = $(shell uname -r)

and build the module - it worked for me.
jl66Consultant

Author

Commented:
[root@localhost Download]# yum localinstall kernel-2.6.27.21-170.2.56.fc10.i686.rpm kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm
Loaded plugins: refresh-packagekit
Setting up Local Package Process
Examining kernel-2.6.27.21-170.2.56.fc10.i686.rpm: kernel-2.6.27.21-170.2.56.fc10.i686
Marking kernel-2.6.27.21-170.2.56.fc10.i686.rpm as an update to kernel-2.6.27.5-117.fc10.i686
kernel-2.6.27.21-170.2.56.fc10.i686.rpm: does not update installed package.
Examining kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm: kernel-devel-2.6.27.21-170.2.56.fc10.i686
Marking kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm as an update to kernel-devel-2.6.27.19-170.2.35.fc10.i686
kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm: does not update installed package.
Resolving Dependencies
--> Running transaction check
---> Package kernel-devel.i686 0:2.6.27.21-170.2.56.fc10 set to be installed
---> Package kernel.i686 0:2.6.27.21-170.2.56.fc10 set to be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package
   Arch Version                   Repository                               Size
================================================================================
Installing:
 kernel
   i686 2.6.27.21-170.2.56.fc10   kernel-2.6.27.21-170.2.56.fc10.i686.rpm  50 M
 kernel-devel
   i686 2.6.27.21-170.2.56.fc10   kernel-devel-2.6.27.21-170.2.56.fc10.i686.rpm
                                                                           18 M

Transaction Summary
================================================================================
Install      2 Package(s)        
Update       0 Package(s)        
Remove       0 Package(s)        

Total download size: 68 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test


Transaction Check Error:
  package kernel-2.6.27.21-170.2.56.fc10.i686 is already installed
  package kernel-devel-2.6.27.21-170.2.56.fc10.i686 is already installed

Error Summary
-------------
Top Expert 2005

Commented:
what about make now (changing the KVERSION line) ?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.