[英]Pause GNU Make in a Windows console if an error occurs
Part of the install for an app I am responsible for, compiles some C code libraries. 我负责的应用程序的部分安装,编译一些C代码库。 This is done in a console using GNU Make. 这是在使用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. 因此,作为安装的一部分,控制台窗口弹出打开,您在编译和链接时看到make文件输出,完成后控制台窗口关闭并且安装程序继续。
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. 然后,在您有机会弄清楚发生了什么之前,make文件会出错并且控制台窗口关闭。
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. 所以,我想要发生的是让控制台窗口暂停,按下“按键继续”类型功能,如果makefile出错,那么控制台保持打开状态。 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. 我无法弄清楚如何在GNU Makefile中或从可以运行Make的批处理文件中执行此操作。
this should do the trick: 这应该做的伎俩:
if not ERRORLEVEL 0 pause
type help if
in DOS for more info on errorlevel usage. help if
在DOS中键入help if
有关errorlevel用法的更多信息。
This is what you're looking for: 这就是你要找的东西:
if ERRORLEVEL 1 pause
If you type 如果你输入
HELP IF
you get this info: ERRORLEVEL number | 你得到这个信息:ERRORLEVEL号码| 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: 使用这个简单的C程序来操作退出代码:
#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
条件 : 0 => 0 == TRUE
When ERRORLEVEL = 0
, the pause will occur because the logic is >=
or greater-than-or-equal . 当ERRORLEVEL = 0
,将发生暂停,因为逻辑>=
或大于或等于 。 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. 请注意,替换1 => 0
也将为真,因此也会发生暂停。 This is true for any positive number. 任何正数都是如此。
We can trigger the opposite effect only by going below 0
: 我们只能通过低于0
来触发相反的效果:
test.exe -1
IF ERRORLEVEL 0 PAUSE
Condition : -1 => 0 == FALSE
条件 : -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: 由于ERRORLEVEL
为1
通常表示存在错误, 0
没有错误,因此我们可以在比较条件中增加最小值以获得我们想要的结果:
test.exe 0
IF ERRORLEVEL 1 PAUSE
Condition : -1 => 1 == FALSE
条件 : -1 => 1 == FALSE
Condition : 0 => 1 == FALSE
条件 : 0 => 1 == FALSE
Condition : 1 => 1 == TRUE
条件 : 1 => 1 == TRUE
In this example. 在这个例子中。 the script will pause when ERRORLEVEL
is 1
or higher 当ERRORLEVEL
为1
或更高时,脚本将暂停
Notice that this allows -1
exit codes the same as 0
. 请注意,这允许-1
退出代码与0
相同。 What if one only wants 0
to not pause? 如果只想让0
不暂停怎么办? We can use a separate syntax: 我们可以使用单独的语法:
test.exe 0
IF NOT %ERRORLEVEL% EQU 0 PAUSE
Condition : -1 != 0 == TRUE
条件 : -1 != 0 == TRUE
Condition : 0 != 0 == FALSE
条件 : 0 != 0 == FALSE
Condition : 1 != 0 == TRUE
条件 : 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. 在此示例中,如果%ERRORLEVEL%
不为0
,脚本将暂停。我们可以通过使用EQU运算符首先检查%ERRORLEVEL% EQU 0
,然后使用NOT运算符获得相反的效果,相当于!=
运算符。 However, I believe this only works on NT machines, not plain DOS. 但是,我相信这只适用于NT机器,而不是纯DOS。
References: 参考文献:
http://chrisoldwood.blogspot.ca/2013/11/if-errorlevel-1-vs-if-errorlevel-neq-0.html http://ss64.com/nt/errorlevel.html 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.