Hi.
This is not so much C related as it is gcc related, I hope it's appropriate here.
When you compile a C program with gcc, and the compilation fails, then gcc returns 1:
test.c:
Code:
int main() {
printf("x is: %d", x);
return 0;
}
Code:
#! /bin/bash
$ gcc test.c
test.c:8:21: error: ‘x’ undeclared (first use in this function)
printf("x is: %d", x);
^
test.c:8:21: note: each undeclared identifier is reported only once for each function it appears in
$ echo $?
1
$
On the other hand, when it compiles without any errors, it'll return 0:
Code:
int main() {
int x=5;
printf("x is: %d", x);
return 0;
}
Code:
#! /bin/bash
$ gcc test.c
$ echo $?
0
$
However, when the compiler issues a warning, it also returns 0:
Code:
int main() {
int x=5;
printf("x is: %d");
return 0;
}
Code:
#! /bin/bash
$ gcc test.c
test.c: In function ‘main’:
test.c:10:2: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
printf("x is: %d");
^
$ echo $?
0
$
Can I somehow change this behaviour?
More specifically, I'd like gcc to return 2 (or any other value other than 0 or 1) when it issues warning(s).
Is it possible?
The reason it will be useful to me is that I plan to write a shell (bash) script that will compile C files, and will react differently depending on if the compilation was successful, was erroneous, or with warnings.
Any help would greatly appreciated.
Regards.