4

I'm trying to link my kernel module with an external static lib, like this:

obj-m += my_prog.o
my_prog-objs := some/path/lib.a
# all the standard targets...

For some reasone, the above Makefile doesn't compile my_prog.c at all, and the resulting module doesn't contain its code. Certainly, if I remove my_prog-objs line, my_prog.c gets compiled.

What's wrong with such an approach in a Makefile?

Igor R.
  • 14,716
  • 2
  • 49
  • 83

3 Answers3

4

You must create a synthetic name as well as the source file and it's object name. You can not use my_prog.o directly as there are rules to make it from source. Here is an sample,

 obj-m += full.o
 full-src := my_prog.c
 full-objs := $(full-src:.c=.o) lib.o # yes, make it an object.

Libraries are only supported from some special directories. Your object should be named lib.o_shipped and placed in the same directory. So, you need to take the external library and provide it locally as a shipped version. You need two object files; one is your compiled 'C' code/driver and the other is it linked together with the library.


The above is relevant to 2.6.36 kbuild infra-structure. The current documentation is in modules.rst section 3.3 Binary Blobs. I think the technique above will still work for libraries as opposed to just objects.

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • Obviously, you only want needed objects in the `lib.o_shipped`. You can create this from the `lib.a` by extracting all objects with `ar` and then doing an incremental link with `ld -r`. At least that is what I think I did... If someone has a better way, I am listening :) – artless noise Mar 03 '14 at 21:04
3

When you create the my_prog-objs list, you tell kbuild to use only the object files in that list. kbuild will no longer compile my_prog.c, and including my_prog.o in my_prog-objs results in a circular dependency. Instead, you need to create a unique obj-m and include both my_prog.o and /path/lib.a in its objs list. For example:

obj-m += foo.o
foo-objs += my_prog.o /path/lib.a

Took me about 2 hours to figure out why my module was doing nothing!

Marc J
  • 1,303
  • 11
  • 11
1

You're overriding the default my_prog-objs, which is just my_prog.o. Instead of replacing the contents with the library, add the library to the default:

my_prog-objs := my_prog.o some/path/lib.a

Hopefully you're not trying to link against a general userspace library... that won't work at all in kernelspace.

Peter
  • 14,559
  • 35
  • 55