如果发生错误,在 Windows 控制台中暂停 GNU Make
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/91263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pause GNU Make in a Windows console if an error occurs
提问by Simon Peverett
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.
我负责的应用程序安装的一部分,编译了一些 C 代码库。这是在使用 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. Otherwise, just exit as normal and close the console.
因此,如果 makefile 中出现错误以便控制台保持打开状态,我想要发生的是让控制台窗口暂停并带有“按一个键继续”类型的功能。否则,只需正常退出并关闭控制台。
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 的批处理文件中执行此操作。
回答by Amir Arad
this should do the trick:
这应该可以解决问题:
if not ERRORLEVEL 0 pause
type help if
in DOS for more info on errorlevel usage.
输入help if
DOS 以获取有关错误级别使用的更多信息。
回答by Benja
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 thanthe number specified.
你会得到这个信息:ERRORLEVEL number | 如果上次运行的程序返回的退出代码等于或大于指定的数字,则指定真条件。
回答by bryc
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 filelike 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. This is important, as it's not immediately clear that the condition is not a ==
comparison.
当 时ERRORLEVEL = 0
,会发生暂停,因为逻辑是>=
or大于或等于。这很重要,因为目前还不清楚条件不是==
比较。
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.
请注意,替换 for1 => 0
也是正确的,因此也会发生暂停。对于任何正数都是如此。
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
of1
通常意味着有错误,并且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
. What if one only wants 0
to not pause? We can use a separate syntax:
请注意,这允许-1
退出代码与0
. 如果一个人只想0
不暂停怎么办?我们可以使用单独的语法:
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. However, I believe this only works on NT machines, not plain DOS.
在这个例子中,如果%ERRORLEVEL%
不是,脚本会暂停0
我们可以通过使用 EQU 运算符首先检查 if %ERRORLEVEL% EQU 0
,然后使用 NOT 运算符来获得相反的效果,相当于!=
运算符。但是,我相信这仅适用于 NT 机器,而不适用于普通的 DOS。
References:
参考:
http://chrisoldwood.blogspot.ca/2013/11/if-errorlevel-1-vs-if-errorlevel-neq-0.htmlhttp://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
回答by bryc
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
- 有关“暂停”的更多信息:http: //technet.microsoft.com/en-us/library/bb490965.aspx
- DOS 命令行参考 AZ:http: //technet.microsoft.com/en-us/library/bb490890.aspx