0

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

1
  • Do you mean if the make command fails, or if one of the commands inside one of the targets fails? Commented Jul 30, 2010 at 21:54

1 Answer 1

2

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; \
}
Sign up to request clarification or add additional context in comments.

1 Comment

Neat! Exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.