Nuno Santos
Nuno Santos

Reputation: 1478

Kernel module makefile - How to make a static library from objects and then link with another object to make the final module

I have the following Makefile for a kernel module:

EXTRA_CFLAGS+=-DLINUX_DRIVER -mhard-float

obj-m += main.o
other-objs := Obj.o Obj1.o Obj2.o Obj2.o ... 

Question:

How can I first make a static lib from all the objects and only then link with the main object with the created static lib?

I know how to make this process manually in two steps. First I call the version above. then I call:

ar rcs libother.a Obj.o Obj1.o ...

And then I change the makefile to:

EXTRA_CFLAGS+=-DLINUX_DRIVER -mhard-float

obj-m += main.o libother.a

Since I don't master Makefiles I wonder if anyone knows a quick and clean solution for this.

Thanks,

Nuno

Upvotes: 1

Views: 4467

Answers (3)

Nuno Santos
Nuno Santos

Reputation: 1478

I will answer to my own question. I found in Kernel Makefile documentation the following:

3.5 Library file goals - lib-y Objects listed with obj-* are used for modules, or combined in a built-in.o for that specific directory. There is also the possibility to list objects that will be included in a library, lib.a. All objects listed with lib-y are combined in a single library for that directory.

So, what I did was to change the make file to look like this:

EXTRA_CFLAGS+=-DLINUX_DRIVER -mhard-float

obj-m += main.o lib.a
lib-y := Obj.o Obj1.o Obj2.o Obj2.o ...

I don't knwo why, but the suggested answers were making the makefile "forget" it was a kernel makefile and all the kernel includes and addition defines in the extra flags were being ignored.

Hope it helps someone in the future,

With my best regards,

Nuno Santos

Upvotes: 2

Pavel Zhuravlev
Pavel Zhuravlev

Reputation: 2791

You can do it like this:

#your variable definitions here

all: your_module_name

your_module_name: main.o libother.a
    gcc $(YOUR_OPTIONS_HERE) -o your_module_name main.o -L. -lother

libother.a: $(other-objs)
    ar rcs $@ $^

main.o: main.c 

#your .o file dependencies here, if needed

.PHONY: all

This way your module will depend on both main.o and libother.a, while libother.a will be remade whenever any of other-objs is changed

Note that the library is linked via -lother option (as it is usually done with libraries). You will also need to specify that the current directory should be included in library search path with -L.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363757

Add this rule to your Makefile:

libother.a: $(other-objs)
    ar cr $@ $<

Upvotes: 0

Related Questions