Search code examples
makefileexit-code

Have make command exit 1


I'm trying to throw an error with Make command. This is my makefile:

exit:
    val=1
    exit $(val)

However, when I run make exit && echo $? it gave me exit code 0. I was expecting exit code 1.

Did I use it wrong? Appreciate it.


Solution

    1. Makefile target "scripts" are not run like real scripts, instead each command is run in a separate shell.
    2. You need to escape dollar signs in makefiles by using an extra dollar sign.
    3. There is a fundamental difference between makefile variables, declared outside of targets and referenced using $(name), and shell variables, declared as part of a command inside a target and referenced using $${name}.

    What you want is either a single command equivalent to val=1 && exit $val:

    exit:
        val=1 && exit $${val}
    

    or a makefile variable used in a simple command:

    val = 1
    exit:
        exit $(val)