Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have written one driver for one device in Linux. How can I create (using gcc) a .ko file so that I can insert it into the kernel?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

Create a Makefile like this.

ifneq ($(KERNELRELEASE),)
obj-m   := mymodule.o
else
KDIR    := /lib/modules/$(shell uname -r)/build
PWD     := $(shell pwd)
all:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
install:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install
%:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) $@
endif

Assuming your module's source is in mymodule.c, running make will create mymodule.ko.

share|improve this answer
    
That fine...But i want to know which option is specifically given to build it as *.ko like -c for *.o , or -fpic -shared for *.so for gcc.. –  Renjith G Dec 8 '10 at 7:01
    
I don't think gcc have such option. Either .ko is just another name for *.a/.so or (what is more probable) it is postprocessed by some script. –  Maciej Piechotka Dec 8 '10 at 22:17
2  
@Renjith G: Exactly how a .ko is made may change from kernel release to kernel release, and the only way to make sure you work with the kernel is to use the kernel's Makefiles. –  ephemient Dec 8 '10 at 22:54
    
Then can u explain me the meaning of each sentence in the above make file? –  Renjith G Dec 9 '10 at 3:56

Short answer: you can't. A .ko file is more than just a compiled version of your driver source. The kernel build system includes some extra information for instance (compiler version, module dependencies, ...).

You can probably dig through the kernel build system to find all required operations, but you shouldn't. It will almost certainly break with the next kernel release, or the one after that. You should use the makefile ephemient supplied.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.