How to test if gcc has failed to compile a program in Windows Batch File (cmd)?

Stylish

New Member
#1
I have made this random C code (app.c)
Mã:
int main()
{
    ERROR; // Just a random code to make sure the compiler fails.
}
and this batch file (run.bat)
Mã:
@echo off
:start
cls
echo Compiling...
gcc app.c -o app.exe
app.exe
pause
goto start
When I double-click run.bat, it gives me the following output:
Mã:
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 . . .
You can notice the last error:
Mã:
'app.exe' is not recognized as an internal or external command,
    operable program or batch file.
That's because there's no app.exe since the compiler has failed to compile it. To prevent that last error from happening, I wanted to check if gcc has succeeded and if so, run the app.
I searched in SO about checking the return values of a program in Batch and then I learned a thing called errorLevel so I tried to use it.
and this is the new run.bat file:
Mã:
@echo off
:start
cls
echo Compiling...
gcc app.c -o app.exe
if %errorlevel% == 0
(
    cls
    app.exe
)
pause
goto start
It immediately exits the app after printing 'Compiling...', I guess I do it wrong maybe..
What's the right way to test if GCC has failed to compile a program in Windows?
 

Admin

Administrator
Thành viên BQT
#2
Firstly, please rename your file to myrun.bat and not run.bat. Let's give gcc time to compile properly:
Mã:
@echo off
:start
cls
echo Compiling...
gcc app.c -o app.exe
timeout 5
:wait
if exist app.exe (app.exe) else (timeout 5 && goto wait)
pause
goto start
Lastly, is your executable actually called app.exe or is it a file containing whitespace? i.e my app.exe

As per my comment, you can start gcc and wait for it.
Mã:
@echo off
:start
cls
echo Compiling...
start /b /w gcc app.c -o app.exe
app.exe
pause
goto start
Lastly. If statements containing parenthesis needs to be on the same line. As well as for else statements. So change:
Mã:
if %errorlevel% == 0
(
    cls
    app.exe
)
To
Mã:
if %errorlevel% == 0 (
    cls
    app.exe
)
 
OP
OP
S

Stylish

New Member
#3
This worked but why should I don't have to wait for 5 seconds (unless I want to check any warnings). In this example, there's no need
 

Admin

Administrator
Thành viên BQT
#4
I added the wait simply because I can see that app.exe does not yet exist at the time you want to run it. Althernatively, start and wait for gcc. let me update answer. See second example.
 
OP
OP
S

Stylish

New Member
#5
After looking at the if-condition in your code, now I understand the problem and it turns out it's so simple and a silly thing from Microsoft at the same time... In the if-condition I posted in this question, I've made two braces in seperate lines, all it needed is to put the first brace ( at the end of the if %errorlevel% == 0 line... Note that it should be a space between the ( and the 0 which is another silly thing from Microsoft... then It worked as expected. Can you update your answer to that so I can accept it?
 
OP
OP
S

Stylish

New Member
#7
The note you added is so close. So I'll add a code to your Answer to explain what I meant and then accept it (Code explains hundreds... words?). Feel free to edit your aswer in your own way after that. Thanks for your help :)
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top