Pause GNU Make in a Windows console if an error occurs
Question
Part of the install for an app I am responsible for, compiles some C code libraries. This is done in a console using GNU Make.
So, as part of the install, a console window pops open, you see the make file output wiz by as it compiles and links, when finished the console window closes and the installer continues.
All good, unless there is a compilation error. Then the make file bugs out and the console window closes before you have a chance to figure out what is happening.
So, what I'd like to happen is have the console window pause with a 'press a key to continue' type functionality, if there is an error from the makefile so that the console stays open. Otherwise, just exit as normal and close the console.
I can't work out how to do this in a GNU Makefile or from a batch file that could run the Make.
Solution
this should do the trick:
if not ERRORLEVEL 0 pause
type help if
in DOS for more info on errorlevel usage.
OTHER TIPS
This is what you're looking for:
if ERRORLEVEL 1 pause
If you type
HELP IF
you get this info: ERRORLEVEL number | Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.
Using this simple C program to manipulate the exit code:
#include <stdio.h>
main(int argc, char *argv[]) {
if (argc == 2) {
// return integer of argument 1
return strtol(argv[1], NULL, 10);
}
else {
return 0;
}
}
We can test the exit code in a batch file like so:
test.exe 0
IF ERRORLEVEL 0 PAUSE
Condition: 0 => 0 == TRUE
When ERRORLEVEL = 0
, the pause will occur because the logic is >=
or greater-than-or-equal. This is important, as it's not immediately clear that the condition is not a ==
comparison.
Notice that subsituting for 1 => 0
will also be true, and thus the pause will occur as well. This is true for any positive number.
We can trigger the opposite effect only by going below 0
:
test.exe -1
IF ERRORLEVEL 0 PAUSE
Condition: -1 => 0 == FALSE
Since an ERRORLEVEL
of 1
typically means there is an error, and 0
no error, we can just increase the minimum in the comparison condition to get what we want like so:
test.exe 0
IF ERRORLEVEL 1 PAUSE
Condition: -1 => 1 == FALSE
Condition: 0 => 1 == FALSE
Condition: 1 => 1 == TRUE
In this example. the script will pause when ERRORLEVEL
is 1
or higher
Notice that this allows -1
exit codes the same as 0
. What if one only wants 0
to not pause? We can use a separate syntax:
test.exe 0
IF NOT %ERRORLEVEL% EQU 0 PAUSE
Condition: -1 != 0 == TRUE
Condition: 0 != 0 == FALSE
Condition: 1 != 0 == TRUE
In this example, the script pauses if %ERRORLEVEL%
is not 0
We can do this by using the EQU operator to first check if %ERRORLEVEL% EQU 0
, then the NOT operator to get the opposite effect, equivalent to the !=
operator. However, I believe this only works on NT machines, not plain DOS.
References:
http://chrisoldwood.blogspot.ca/2013/11/if-errorlevel-1-vs-if-errorlevel-neq-0.html http://ss64.com/nt/errorlevel.html
Have you tried the 'pause' command?
@echo off
echo hello world
pause
- more info on 'pause' : http://technet.microsoft.com/en-us/library/bb490965.aspx
- DOS Command Line reference A-Z : http://technet.microsoft.com/en-us/library/bb490890.aspx