Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know that I can interrupt a make process anytime without having to recompile the entire source tree again. As I know, make only compiles a target if it's not compiled yet, or the source code is modified after the last compilation.
But if I interrupt make, there will surely be one or more (depending on the concurrency level) half-ready binaries. What does it do with them the next time I run make? Or does it finish the current target when I press Ctrl+C to avoid partly compiled binaries?

share|improve this question
1  
Mostly you only need to worry if the computer powered off unexpectedly. A few times my Ubuntu managed to get in a kernel deadlock (or whatever it is) and left half-ready binaries, which ended up wasting more than two hours. –  Alvin Wong Jun 19 at 14:42

3 Answers 3

up vote 10 down vote accepted

In simple terms, you can think of make as having a (possibly large) number of steps, where each step takes a number of files as input and creates one file as output.

A step might be "compile file.c to file.o" or "use ld to link main.o and file.o into program". If you interrupt make with CtrlC, then the currently executing step will be terminated which will (or should) remove the output file it was working on. There are usually not any "half-ready binaries" left behind.

When you restart make, it will look at the timestamps of all the input and output files and rerun the steps where:

  • an input file has a newer timestamp than the output file
  • the output file does not exist

This generally means that if a step takes a long time to run (it's rare on modern computers, but the ld step for large programs could easily take many minutes when make was designed), then stopping and restarting make will start that step over from the beginning.

The reality of your average Makefile is considerably more complicated than the above description, but the fundamentals are the same.

share|improve this answer

Ctrl+C causes a SIGINT to be sent to the process running. This signal can be caught by the process. In the make source code you can find a trap for this signal in commands.c:

  /* If we got a signal that means the user
     wanted to kill make, remove pending targets.  */

  if (sig == SIGTERM || sig == SIGINT

  ... remove childrens ...

  /* Delete any non-precious intermediate files that were made.  */

  remove_intermediates (1);

remove_intermediates() is the cleanup function of make, see it's definition here:

/* Remove all nonprecious intermediate files.
   If SIG is nonzero, this was caused by a fatal signal,
   meaning that a different message will be printed, and
   the message will go to stderr rather than stdout.  */

And later in the function you see, they will be effectively deleted:

status = unlink (f->name);

Conclusion: Generally don't be affraid of interrupting a compilation with make. If it's not an uncatchable signal (SIGKILL, SIGSEGV, SIGSTOP) it will do a cleanup of intermediate files.

share|improve this answer
    
SIGSEGV is catchable on many Unices. –  Chris Down Jun 19 at 10:02

When something stops make (be it ctrl-C, shutdown, or even a command that fails), the work already done stays. When restated, make does as always: it figures out what still needs to be done (because a file changed or make never got to have it processed doesn't matter) and goes on with the job.

The above description clearly presumes the relevant Makefiles describe the dependencies and commands to execute correctly, so all what needs to be (re)made is.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.