Pages

Makefies : Conditional execution using ifeq

Conditions can be added in makefiles using "if" conditions, to ensure the commands get executed only under certain conditions.

The syntax of if in a makefile is



Let us say we have two files hello1.c and hello2.c which we want to compile using makefile. But the condition for compiling these files is that if the user who is compiling is root then hello_1.c should be compiled,else for any other user hello_2.c should be compiled. We can use the if condition in makefile to achieve this.

This can be done using the following makefile





Above statement assigns the current user name to the variable "user" using the command "whoami".

Then we use the "if" condition and check the user name and assign the target based on the result of the condition check.

If we execute the make command as root we will get the following output



If we do the same as any other user other than root, we will get the following output




1 comment:

  1. Thanks for taking the time to discuss this I feel strongly about it and love learning more on this topic. If possible as you gain expertise would you mind updating your blog with more information? as it is extremely helpful for me.
    we buy houses fast

    ReplyDelete