Compile a Linux module consisting of multiple source files

Certainly a lot of you know very well how to make this action but since I’m not very expert in makefile syntax I spend some time to figure out to to compile a Linux module consisting of multiple source files. Just to avoid the same waste of time to others who, like me, have no big experience in this sector I’ll post a short explanation about how to get this result.

Basically the name of the source file to compile for generate a module is specified inside the makefile as:

obj-m := hello.o

The module generated will have the same name like hello.ko. This will work very well if you have only a single source file in your module project (in this example the file is hello.c) but if your module project consists of more than one source file you have to use a different approach. The “trick” is to use an additional “label” for group all the source files you have and use this label as “virtual” file to pass to the main makefile instruction just checked before. A simple example will clarify.

obj-m := main_hello.o
main_hello-objs := hello1.o hello2.o

These instructions will produce a module called main_hello.ko created compiling the two source files hello1.c and hello2.c. In this case the important thing is the label used for group the source files need to be “invented” using a word that must not match the name of any source file in the project.

If you want to have more information regarding Linux module you can read also The Linux Kernel Module Programming Guide


Post a Comment