Condition implementation in MAKEFILE.

This post will explain you how to write MAKE file in which we can check conditional compilation.

In Makefile "P32" is the value on which we deciding which code we need to compile.

Basically this will need to implement when we do compilation of 32bit or 64bit machine.

Makefile:-

#TEST=P64
TEST=P32
CCDEF=-D$(TEST)
all :
ifeq ($(TEST),P32)
        echo "Success"
        gcc $(CCDEF) -o test test.c
else
        echo "Failed"
        gcc  $(CCDEF) -o test test.c
endif
 
int main(int argc, char *argv[])
{
#if P64
printf("this is 64 bit");
#else
printf("this is 32 bit");
#endif
return 0;
}

In Above code we can use #ifdef instead of #if.

Difference between #ifdef and #if:-

 
  #ifdef  only cares if MACRO has been defined or not. Value doesn't matter.
 
  #if  checks for the value of MACRO and evaluates MACRO accordingly.

Let me define this with help of example.

#define FOO 0
#if FOO
  // this code will not compile, if FOO value is 0
#endif
#ifdef FOO
  // this code will compile, if FOO value is 0, #ifdef not check value is 0 or 1 it only
  // check value is define or not.
#endif
Please follow and like us:
RSS
Follow by Email
Facebook
LinkedIn
Google+

Leave a Reply

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