Discussion:
Catching non-C89-compliant declarations
(too old to reply)
Marvin Humphrey
2006-04-11 00:28:26 UTC
Permalink
Greets,

I regularly test my modules on OS X, RedHat 9, and FreeBSD 5.3 before
releasing. On all of these systems, the C compiler allows the
declaration of variables anywhere in a block. I try to write C89
compliant code, but sometimes I slip up and none of them will catch
it. Are there any flags I could pass to the C compiler to force it
to choke on variable declarations that are illegal under C89?

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/
Marvin Humphrey
2006-04-11 01:16:17 UTC
Permalink
Post by Marvin Humphrey
Greets,
I regularly test my modules on OS X, RedHat 9, and FreeBSD 5.3
before releasing. On all of these systems, the C compiler allows
the declaration of variables anywhere in a block. I try to write
C89 compliant code, but sometimes I slip up and none of them will
catch it. Are there any flags I could pass to the C compiler to
force it to choke on variable declarations that are illegal under C89?
-Wall, -pedantic among others.... Thanks for the private replies.

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/
Marcus Holland-Moritz
2006-04-19 17:25:54 UTC
Permalink
Post by Marvin Humphrey
Post by Marvin Humphrey
Are there any flags I could pass to the C compiler to
force it to choke on variable declarations that are illegal under C89?
-Wall, -pedantic among others.... Thanks for the private replies.
Try -std=c89 if you're using gcc:

$ cc -std=c89 -pedantic -o const const.c
const.c: In function `main':
const.c:9: warning: ISO C90 forbids mixed declarations and code

If you want it to choke, try adding -Werror.

HTH,
Marcus
s***@fisharerojo.org
2006-05-10 14:03:43 UTC
Permalink
More recent Perl's compiled with gcc should have
-Wdeclaration-after-statement. Also, you can add it as a ccflag in
Makefile.PL.

***@kirk:~$ cat test.c
int
main() {
for(int i = 0; i < 5; i++);
int j = 0;
}
***@kirk:~$ cc -o test -Wdeclaration-after-statement test.c
test.c: In function 'main':
test.c:3: error: 'for' loop initial declaration used outside C99 mode
test.c:4: warning: ISO C90 forbids mixed declarations and code

Loading...