代码之家  ›  专栏  ›  技术社区  ›  Beyondo

如何测试gcc是否在Windows批处理文件(cmd)中编译程序失败?

  •  0
  • Beyondo  · 技术社区  · 6 年前

    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
    

    当我双击 ,它提供以下输出:

    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 因为编译器未能编译它。 防止 成功 运行应用程序 .

    我搜索了很多关于批量检查程序返回值的信息,然后我学到了一个叫做 errorLevel 所以我试着用它。

    这是新的 文件:

    @echo off
    :start
    cls
    echo Compiling...
    gcc app.c -o app.exe
    if %errorlevel% == 0
    (
        cls
        app.exe
    )
    pause
    goto start
    

    立即退出 '打印后的应用程序' Compiling... '我想我做错了也许。。

    如果GCC未能在Windows中编译程序,正确的测试方法是什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Gerhard    6 年前

    myrun.bat 而不是 run.bat . 让我们给gcc时间来正确编译:

    @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
    

    最后,你的可执行文件真的被调用了吗 app.exe 或者是一个包含空格的文件?即 my app.exe

    根据我的意见,你可以启动gcc并等待它。

    @echo off
    :start
    cls
    echo Compiling...
    start /b /w gcc app.c -o app.exe
    app.exe
    pause
    goto start
    

    最后。包含括号的If语句需要在同一行上。以及else语句。所以改变一下:

    if %errorlevel% == 0
    (
        cls
        app.exe
    )
    

    if %errorlevel% == 0 (
        cls
        app.exe
    )