How to check if gcc has failed, returned a warning, or succeeded in Bash? How to check if gcc has failed, returned a warning, or succeeded in Bash? bash bash

How to check if gcc has failed, returned a warning, or succeeded in Bash?


Your condition should be:

if [ $? -ne 0 ]

GCC will return zero on success, or something else on failure. That line says "if the last command returned something other than zero."


if gcc helloworld.c -o helloworld; then echo "Success!";else echo "Failure"; fi

You want bash to test the return code, not the output. Your code captures stdout, but ignores the value returned by GCC (ie the value returned by main()).


To tell the difference between compiling completely cleanly and compiling with errors, first compile normally and test $?. If non-zero, compiling failed. Next, compile with the -Werror (warnings are treated as errors) option. Test $? - if 0, it compiled without warnings. If non-zero, it compiled with warnings.

Ex:

gcc -Wall -o foo foo.cif [ $? -ne 0 ]then    echo "Compile failed!"    exit 1figcc -Wall -Werror -o foo foo.cif [ $? -ne 0 ]then    echo "Compile succeeded, but with warnings"    exit 2else    echo "Compile succeeded without warnings"fi