0

Can any one explain the simple makefile give below, as i want to start with kernel module programming and i am not getting it.

obj−m += hello−1.o

all:
    make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules

clean:
    make −C /lib/modules/$(shell uname −r)/build M=$(PWD) clean
  • Can you be a bit more precise, what you are after? We do not know if there is a specific question you seem to have regarding to the example Makefile or you need a generic introduction to Makefiles. In the latter case I do not see the reason why starting with kernel development (not easy). – Christian Wolf Feb 25 at 8:56
  • @ChristianWolf yup i need generic introduction to makefile,like i know it is used to recompile the modules again and again in an easy way. But i havent been able to understand the each and every line of the makefile i have mentioned in question? – Sunny Khandare Feb 25 at 9:03
  • The -C option tells make to change to some dir before doing anything else -- so you should look at the Makefile from /lib/modules/$(uname −r)/build ;-) (hint: there are some docs in the Documentation/kbuild/ dir in the kernel source) – mosvy Feb 25 at 18:20
  • 1
    If you're completely new to (GNU) make, I suggest reading its manual. It's pretty good. – mosvy Feb 25 at 18:48
1

In fact you have two targets (all and clean). You can call them on the console when you are in the folder by make all or make clean. The first target is alwas the default target (make results in make all).

Indented the recipe is stated. This is the code that gets executed by make to build the target. So the invocation of make will result in the invocation of

make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules

Of course the shell expands the uname command and the M environment variable. Then make is invoked in the named folder (/lib/modules/$(shell uname −r)/build) with the target modules. What this target (of the subprocess) does is obviously not specified in this file. This means, that in /lib/modules/$(shell uname −r)/build there is another Makefile, that has a modules target defined. The above listed command builds/calls/issues this modules target in /lib/modules/$(shell uname −r)/build/Makefile.

The same holds true for the clean target. This will issue a subprocess with the clean target in the named folder.

The idea of the two targets is to have two distinguished features. One (all) is to build something (your module probably). The other is to clean up your work (clean) in case the compiling went wrong and you want to remove any non-source files. This interpretation of the targets is however only semantically from the name. It depends upon the implementation of them. You can name them t1, t2, and t3 if you like. However is is much less readable than all, clean, and install.

The first line appends a string hello−1.o to the vaiable obj−m. As the variable is empty before (if not set up by external environmental variables), it is simply set to that value. However, the value is not exported, thus only locally visible. So it has no effect on the subprocesses (see this link).


An additional remark: I was wrong that the obj-m had no effect. The Makefile is reread by the kernel's Makefile and searched for variables beginning with obj-. These are then used for compiling the required object files. Source: https://stackoverflow.com/a/21140538/882756

  • Why do we need two targets all and clean. And What does modules indicate in "make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules". – Sunny Khandare Feb 25 at 9:27
  • I updated the answer to reflect your additional questions. – Christian Wolf Feb 25 at 11:22
  • thanks for your updation , can you please explain what the words 'modules' and 'clean' mean at the end of targets. – Sunny Khandare Mar 3 at 5:00
  • The words modules and clean are just targets of the kernel's Makefile. These are de-facto standard names. Sorry, I do not get your problem here. They could have been called apple and banana as well, but the kernel developers decided on these target names. – Christian Wolf Mar 4 at 15:46
  • 1
    Thank You Christian , learnt a lot from you. – Sunny Khandare Mar 7 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.