Hello World Makefile

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












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









share|improve this question
























  • 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















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









share|improve this question
























  • 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













0












0








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









share|improve this question
















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






make






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 25 at 8:54









Christian Wolf

24228




24228










asked Feb 25 at 8:52









Sunny KhandareSunny Khandare

72




72












  • 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

















  • 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
















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





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





@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





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




1





If you're completely new to (GNU) make, I suggest reading its manual. It's pretty good.

– mosvy
Feb 25 at 18:48





If you're completely new to (GNU) make, I suggest reading its manual. It's pretty good.

– mosvy
Feb 25 at 18:48










1 Answer
1






active

oldest

votes


















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






share|improve this answer

























  • 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











  • And why are we writing 'M=$(pwd)'.Thank You

    – Sunny Khandare
    Mar 3 at 5:59






  • 1





    Thank You Christian , learnt a lot from you.

    – Sunny Khandare
    Mar 7 at 5:40










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502806%2fhello-world-makefile%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer

























  • 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











  • And why are we writing 'M=$(pwd)'.Thank You

    – Sunny Khandare
    Mar 3 at 5:59






  • 1





    Thank You Christian , learnt a lot from you.

    – Sunny Khandare
    Mar 7 at 5:40















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






share|improve this answer

























  • 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











  • And why are we writing 'M=$(pwd)'.Thank You

    – Sunny Khandare
    Mar 3 at 5:59






  • 1





    Thank You Christian , learnt a lot from you.

    – Sunny Khandare
    Mar 7 at 5:40













1












1








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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 25 at 11:28

























answered Feb 25 at 9:07









Christian WolfChristian Wolf

24228




24228












  • 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











  • And why are we writing 'M=$(pwd)'.Thank You

    – Sunny Khandare
    Mar 3 at 5:59






  • 1





    Thank You Christian , learnt a lot from you.

    – Sunny Khandare
    Mar 7 at 5:40

















  • 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











  • And why are we writing 'M=$(pwd)'.Thank You

    – Sunny Khandare
    Mar 3 at 5:59






  • 1





    Thank You Christian , learnt a lot from you.

    – Sunny Khandare
    Mar 7 at 5:40
















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






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





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





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













And why are we writing 'M=$(pwd)'.Thank You

– Sunny Khandare
Mar 3 at 5:59





And why are we writing 'M=$(pwd)'.Thank You

– Sunny Khandare
Mar 3 at 5:59




1




1





Thank You Christian , learnt a lot from you.

– Sunny Khandare
Mar 7 at 5:40





Thank You Christian , learnt a lot from you.

– Sunny Khandare
Mar 7 at 5:40

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502806%2fhello-world-makefile%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?