Compiling a kernel driver that has multiple source files

By | March 27, 2008

All the drivers that I had dealt with until today where written in just one source file. But today I had several problems trying to compile a driver for a 2.6 kernel that had several source files. Most of the info I found like in the LKMPG and in LDD3, did not work for me. The problem was with my Makefile that I was writing in this way (the tabs after the all and clean targets were removed due to formatting issues):

The files in my dir were:

If you follow the LKMPG or other sources you see that this seems correct: I wanted to compile a couple of files, modmain.c that contains the module init/exit funcs, and file.c that contains funcs used in modmain.c, and generate a driver module modmain.ko..
After compiling and linking without problems, I insmod the module but I didn’t see any of the printk() I wrote, in fact, the driver can be inserted and removed, but it does nothing.
After a while, I use nm to check which symbols were inside the driver and to my surprise the only symbols inside the .ko were

No signs of init_module and friends. At this point I realized that the file modmain.c was not being compiled. In fact, my output from the Makefile was the following:

modmain.c was not being compiled!

After playing around with the Makefile I got the right file (again, the tabs after the all and clean targets were removed due to formatting issues):

Now, the output when compiling and linking is the following:

modmain.c is being compiled this time!
And nm says the following:

The symbols used in modmain.c appear normally.

I changed the final driver name to mymod.ko and the file modmain.c is now part of the mymod-objs variable, in this way I’m sure that it’s compiled. Notice that the final driver name is different from any of the source files, including the file that contains the module_init/exit funcs.

Since I had the above mentioned issues for compiling a driver that has multiple source files following commonly linux kernel drivers docs and I have seen other people in the same situation, I decided to share the procedure that worked for me, but this does not mean in any way that the references I used were mistaken, most probably I missed something.

4 thoughts on “Compiling a kernel driver that has multiple source files

  1. Nick Formaber

    Thank you very much for this post! It solved the same issue I had!

    Was searching for more than 2 hours (in stackoverflow, etc.) before finding your post 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *