我有以下代码:

@ECHO off
set name=code
set command1="path/to/the/file"
set command2=gcc -o %name%.exe %name%.c
start cmd.exe /c %command1% & %command2%>temp
set msg =<temp
call :CheckEmpty "temp"
goto :eof
:CheckEmpty
if %~z1 == 0 (
%name%.exe
exit
)
echo temp
pause
goto :eof


它应该做的是编译code.c,然后测试编译器的输出是否为空,以及是否运行code.exe。如果不是,则回显错误消息。

我就是无法正常工作。

我查看了“ temp”文件,它为空,尽管由于某种原因它仍然回显错误消息。批处理对我来说很陌生,而CheckEmpty部分是从另一个stackoverflow问题中复制的。

任何帮助将不胜感激!

最佳答案

我在linux上,因此无法测试以下内容,

但是,这是我的写法。

@ECHO off
PUSHD
set name=code

cd path/to/the/file

gcc -c -ggdb -Wall -Wextra -pedantic -std=c99 -o %name%.o   name%.c -I. 2>&1 temp
if errorlevel 1  (goto error)

gcc -ggdb %name%.o -o %name%.exe 2>>&1 temp
if errorlevel 1 (goto error)

%name%.exe
goto end

:error
type temp | more
pause
:end
POPD


并命名这样的文件:(也许)compileit.cmd

然后通过以下方式执行:

cmd.exe /c compileit.cmd


调用gcc时,还有许多其他无法使用的参数。

有关详细信息,请参见:https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html

创建批处理文件时,还有许多其他功能。

有关详细信息,请参见:http://ss64.com/nt/set.html

关于c - 编译C代码,然后检查输出是否为空并运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34162329/

10-09 06:52
查看更多