Issue
By default, gcc doesn't complain about jumping over variable initializations when compiling .c files, while it does when compiling .cpp files. I want it to complain when compiling c files. Is there an easy way to achieve this?
For example, the code below in .c does not produce any warnings/errors, while in .cpp it will:
int main()
{
goto out;
int i = 0;
out:
return 0;
}
result:
gec@ubuntu:~/work/test/json_test$ gcc test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:5:1: error: jump to label ‘out’ [-fpermissive]
out:
^
test.cpp:3:7: note: from here
goto out;
^
test.cpp:4:6: note: crosses initialization of ‘int i’
int i = 0;
^
Solution
Use the -Wjump-misses-init
option (or -Werror=jump-misses-init
if you want it to be a hard error):
$ gcc -Wjump-misses-init try.c
try.c: In function 'main':
try.c:3:9: warning: jump skips variable initialization [-Wjump-misses-init]
goto out;
^~~~
try.c:5:1: note: label 'out' defined here
out:
^~~
try.c:4:13: note: 'i' declared here
int i = 0;
^
Answered By - melpomene