Condition implementation in MAKEFILE.

Connect with

makefile exampleThis post explained about makefile example, in which, how to write a 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 a compilation of 32bit or 64bit machines.

Makefile script

all :
ifeq ($(TEST),P32)
        echo "Success"
        gcc $(CCDEF) -o test test.c
        echo "Failed"
        gcc  $(CCDEF) -o test test.c
int main(int argc, char *argv[])
#if P64
printf("this is 64 bit");
printf("this is 32 bit");
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
#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.

Connect with

Leave a Comment

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