Search code examples
makefileconditional-statementscompilationconditional-compilation

Conditional statements depending on successful compilation within a Makefile


For a makefile, I am trying to make it run a block of code in case of successful compilation, or an else block otherwise.

I have tried something like this

default:
ifeq ($(gcc -obuild main.c), 0)
    echo "successful"
else
    echo "you fail lol"
endif

But I cannot get the compilation command to be evaluated as my code suggests that I want. I thought that it could work like Bash but it seems that not, or I am missing something I dont know.

How can I accomplish this task?


Solution

  • Do it with the shell, not with Make:

    default:
        if gcc -obuild main.c  ; then \                                        
    echo "sucessful" ; \
    else echo "you fail" ; fi
    

    Note that only the first line of the commands (if...) starts with a tab.