views:

22

answers:

1

With GNU Make and one of the compilers in gcc: Is it possible to execute commands if (and only if) the compiling fails?

+1  A: 

If you prefix a command with -, make keeps going even if the command returns a nonzero error code. But there's no way to access the error code from the first command in the second command.

You can write arbitrarily complex shell scripts in a single make command. For example, here is how to call two recovery commands if the C compiler fails, running the second one only if the first one fails, and then stopping the build process if the C compiler failed.

$(CC) $(CFLAGS) -o $@ -c $< || { \
  recovery_command_1 && \
  recovery_command_2; \
  false; \
}
Gilles
Neat! Exactly what I was looking for.
Paul