LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2007, 01:25 AM   #1
pallava
LQ Newbie
 
Registered: Sep 2007
Posts: 1

Rep: Reputation: 0
How to link a static library to a kernel module


Hi
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.

Last edited by pallava; 09-26-2007 at 01:38 AM.
 
Old 09-26-2007, 08:25 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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:
Code:
ifndef KERNELDIR
	KERNELDIR  := /lib/modules/$(shell uname -r)/build
endif

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

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

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

install:
	$(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.

HTH.

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
KDevelop Executable->static link library lucky6969b Programming 1 02-13-2006 04:01 PM
netfilter/iptables in kernel: module vs static -Nw- neX Linux - Security 5 03-25-2005 06:44 PM
how to static link a specific library? koodailar Programming 3 07-26-2004 03:35 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration