2

I have this program in C

#include <stdio.h>

int main() {
    char foo[10];
    int i;
    for(i = 0; i < 20 ; i++) {
        foo[i] = 0;
    }

    return 0;
}

If I run this script

#!/bin/bash
gcc -O3 -o hello hello.c
if [ $? -eq 0 ]
then
    echo -e "\033[1;32mcompilation sucess!\033[0m"
else
    echo -e "\033[1;31mcompilation error!\033[0m"
fi

It will output

hello.c: In function ‘main’:
hello.c:8:10: warning: iteration 10u invokes undefined behavior     [-Waggressive-loop-optimizations]
foo[i] = 0;
    ^
hello.c:6:2: note: containing loop
  for(i = 0; i<20 ;i++)
  ^
compilation sucess!

That is because gcc didn't considered that an error but still output those warnings on stderr.

But I still want to detect that within the bash script.

screenshot

10
  • Welcome to Super User. If your question is about programming/coding, pls post in Stack Overflow. Apr 13, 2018 at 19:31
  • 2
    @ArunVinoth The question is still on topic here, as it is about using a programming tool and it's interaction with the calling script.
    – DavidPostill
    Apr 13, 2018 at 19:33
  • @KamilMaciorowski Not really, because an answer to the question asked asked in the title would aslo resolve the OPs question (even it wouldn't be the best answer).
    – DavidPostill
    Apr 13, 2018 at 19:38
  • i still want to know how to detect if command has printed to stderr, that would be much better than the accepted awnser Apr 13, 2018 at 20:04
  • I think @DavidPostill's answer is the correct one, but if you want to retain the distinction between warnings and errors, as well as detecting notes, you can add ` 2>err` to the end of the gcc command and make your test if [ $? -eq 0 -a ! -s err ].
    – AFH
    Apr 13, 2018 at 20:06

2 Answers 2

2

I think @DavidPostill's answer is the correct one, but if you want to retain the distinction between warnings and errors, as well as detecting notes, you can add 2>err to the end of the gcc command:

gcc -O3 -o hello hello.c

Then make your test:

if [ $? -eq 0 -a ! -s err ]
2
  • well your answer is what i really want and it is very helpful, yeah you are right the way i made my question it seems that i really want to make that detection in that case, but an detection in any case it is much better. Apr 13, 2018 at 20:30
  • since that code i created just to ilustrate the problem. it is actually not a real code that do 'really usefull stuff' Apr 13, 2018 at 20:31
4

gcc didn't consider that as an error

You can use the -Werror switch.

-Werror Make all warnings into errors.

Source Warning Options - Using the GNU Compiler Collection (GCC)

3
  • although it is just a trick that i could use with this gcc command, but what i really want is something that would help me with any command. Apr 13, 2018 at 20:22
  • @gabriel80546 Then you have asked that in your question. But you shouldn't change your question now as it would invalidate existing answers.
    – DavidPostill
    Apr 13, 2018 at 20:23
  • 1
    i thought the title was clear Apr 13, 2018 at 20:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .