我工作的代码库主要是 C 和一点 C++,并且主要是用 gcc 构建的,但有时需要用 MSVC 构建。微软的 C 编译器仍然是 C89,带有一些小的扩展,并且它仍然不支持混合代码和变量定义à la C++/C99。因此,我需要找到一种方法来防止开发人员在使用 gcc 时编写无序代码/变量定义,否则构建随后会因 MSVC 而中断。如果我使用 gcc -std=c89 ,那么一切都会中断,因为不允许使用 C++ 风格的注释(也可能存在其他问题,但我没有进一步研究这一点)。如果我使用 gcc -std=gnu89 ,则允许无序代码/变量定义,因此这对我也没有帮助。有什么想法吗?我想我只需要像 gcc -std=c99 -fno-inline-variable-definitions 之类的东西(如果存在这样的选项)。
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 much C89 with a few minor extensions, and it still doesn't support mixed code and variable definitions à la C++/C99. So I need to find a way to prevent developers from writing out-of-order code/variable definitions while they are working with gcc, otherwise the build subsequently breaks with MSVC. If I use gcc -std=c89
then everything breaks because C++-style comments are not allowed (there may be other issues too, but I haven't looked into this any further). If I use gcc -std=gnu89
then the out-of-order code/variable definitions are allowed, so that doesn't help me either. Any ideas ? I guess I just need something like gcc -std=c99 -fno-inline-variable-definitions
, if such an option existed.
发布评论
评论(3)
您正在寻找
-Wall
-Wextra
-Wstrict-prototypes
-Wold-style-definition
-Wmissing-prototypes
-Wmissing-declarations
和-Wdeclaration-after-statement
选项,如 gcc 警告信息页面。请注意,这些可能会因系统头文件中的问题而导致大量噪音,并且它们只是警告,因此您必须制定热衷于零警告构建的策略。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.我不相信有办法做你想做的事。 MSVC 支持的 C 方言比 C99 更接近 C89(例如,它也不支持指定的初始值设定项);你确实想要一些更类似于 C89-with-C++-comments-and-inline-keyword 的东西。
问题是 C++ 注释会影响有效 C89 代码的正确性。例如,这行的含义发生了很大的变化:
我认为最好的选择是在 C 源文件中强制执行 C89,包括 C89 风格的注释。不过,
inline
可能没问题:您可以在 gcc 上将其定义为__inline
。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:
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.导致代码无法编译的不是 Win32,而是编译器。您可以使用 Win32 上的 GCC 并获得更好的跨平台兼容性。
另一种可能性是使用 C++ 编译来构建 Win32; GCC 编译已经确定它是有效的 C,而 C++ 编译通常也会使其变得更强的 C。
[编辑]
另一种解决方案是使用持续集成服务器,例如配置的 CruiseControl,以便每当 GCC 平台编码员签入时代码后,CI 服务器可以检查并使用 VC++(甚至应用第三方静态分析工具)构建它,并在出现错误时将结果通过电子邮件发送给签入错误代码的用户。该解决方案对于原始问题来说可能是重量级的,但除此之外还可能产生许多其他好处。
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.