我写了一段随机的 C 代码(app.c
)
int main()
{
ERROR; // Just a random code to make sure the compiler fails.
}
还有这个批处理文件(run.bat
)
@echo off
:start
cls
echo Compiling...
gcc app.c -o app.exe
app.exe
pause
goto start
当我双击
run.bat
时,它会输出以下内容:Compiling...
app.c: In function 'main':
app.c:3:2: error: 'ERROR' undeclared (first use in this function)
ERROR; // Just a random code to make sure the compiler fails.
^~~~~
app.c:3:2: note: each undeclared identifier is reported only once for each function it appears in
'app.exe' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
您可以注意到最后一个错误:
'app.exe' is not recognized as an internal or external command,
operable program or batch file.
那是因为编译器未能成功编译,而没有了
app.exe
。为了防止出现这种最后的错误,我想要检查 gcc 是否已经 成功,如果是,则 运行 app 。我在 SO 上搜索有关批处理程序中检查程序返回值的信息,然后了解到了一个名叫 errorLevel
的东西,所以我尝试使用它。这就是新的 run.bat
文件:@echo off
:start
cls
echo Compiling...
gcc app.c -o app.exe
if %errorlevel% == 0
(
cls
app.exe
)
pause
goto start
在打印“Compiling...
”后,它会立即退出应用程序,我猜可能是我的做法有误。
在Windows中,测试GCC是否未能编译程序的正确方法是什么?
run.bat
不是一个好的名称,建议改为myrun.bat
。 - Gerhardcompile.bat
。:) 但它仍然立即退出... - BeyondoERROR;
行,我只能看到已编译的exe
,但在两种情况下它仍然立即退出... - Beyondo