-   Linux - Newbie (
-   -   How to link a static library to a kernel module (

pallava 09-26-2007 01:25 AM

How to link a static library to a kernel module
I am a linux newbie and i want to know whether it is possible to link a static library to a kernal module. if so any sample piece of code (mainly the inclusion of that paticular .a file in make file )is very much thankful. Thanks in advance for u r help.

osor 09-26-2007 08:25 PM

Yes, this should be possible, provided your library archive doesnít depend on any external symbols (e.g., no C-library dependence). There are two things meant by your question, and both are possible. The first is: can you have multiple source files linked to form one single loadable kernel module, much as they can form an archive or executable in userspace? The second is: suppose you have a precompiled (for the target arch) library archive making use of no external symbols. Can you use that object code in a kernel module without the source code? The answer to both is yes, as I will show in this example.

Suppose I want to make a module which will eventually be called foo.ko. The logic is complicated enough to be separated into two separate source files: bar.c and baz.c (this addresses the first question). Additionally, there is a precompiled library filled with symbols referenced by bar.c or baz.c. This is named qux.a (this addresses the second question). Then there is a Makefile to tie it all together (this is the file I will show you). All four of these files live in the same directory (for simplicity), which happens to be somewhere outside the kernel source tree (also for simplicity).

The Makefile might go something like this:

        KERNELDIR  := /lib/modules/$(shell uname -r)/build

obj-m    := foo.o
foo-objs := bar.o baz.o qux.a

        $(MAKE) -C $(KERNELDIR) M=$(PWD)

        $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
        /sbin/depmod -ae

This way you can execute make from the directory containing the sources. A module with prefix foo (full name foo.ko) will be built. The objects from which it is built are bar.o, baz.o and qux.a. Since bar.c and baz.c are newer than bar.o and baz.o (which donít yet exist), they will be compiled into their respective objects. Since qux.a exists (and since there is no requisite), it will be incorporated as-is.


P.S., you can read more about using Makefiles with the kbuild system here.

All times are GMT -5. The time now is 02:40 AM.