How to enforce C89-style variable declarations in gcc?

前端 未结 3 1014
执笔经年
执笔经年 2021-02-07 23:55

I work on a code base which is mostly C with a little C++, and is mostly built with gcc but occasionally it needs to be built with MSVC. Microsoft\'s C compiler is still pretty

相关标签:
3条回答
  • 2021-02-08 00:33

    I don't believe there is a way to do what you want. The dialect of C supported by MSVC is closer to C89 than C99 (eg. it doesn't support designated initializers either); you really want something more akin to C89-with-C++-comments-and-inline-keyword.

    The problem with that is that C++ comments can affect the correctness of valid C89 code. For example, the meaning of this line changes substantially:

    int a = 10//* foo */2;
    

    I'd say your best bet is to enforce C89 in your C source files, including C89-style comments. inline is probably OK, though: you can define it to __inline on gcc.

    0 讨论(0)
  • 2021-02-08 00:37

    It is not Win32 that renders the code uncompilable, but the compiler. You can use GCC on Win32 and get greater cross-platform compatibility.

    Another possibility is to use C++ compilation for your Win32 build; the GCC compilation will already have determined that it is valid C, and C++ compilation will generally make it stronger C too.

    [edit] Another solution is to use a continuous integration server such as CruiseControl configured so that whenever the GCC platform coders check-in code, the CI server can check it out and build it using VC++ (or even apply a third-party static analysis tool) and on error e-mail the results to the user who checked in the erroneous code. This solution may be heavyweight for the original problem, but may yield many other benefits besides.

    0 讨论(0)
  • 2021-02-08 00:39

    You're after the -Wall -Wextra -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations and -Wdeclaration-after-statement options, as described on the gcc warnings info page. Note that these can cause a lot of noise from issues in system header files, and they're only warnings so you've got to have a policy of being keen to have a zero-warning build.

    0 讨论(0)
提交回复
热议问题