Being extremely pedantic with the way your code is compiled

I would like to find out which is the most extreme error checking flag combination for g++ (4.7). We are not using the new C++11 specification, since we need to cross compile the code with older compilers, and these older compilers (mostly g++ 4.0) often cause problems which simply are ignored by the g++4.7.

Right now we use the following set of flags:

-Wall -Wcomment -Wformat -Winit-self -ansi -pedantic-errors 
-Wno-long-long -Wmissing-include-dirs -Werror -Wextra

but this combination does not identify issues such as a double being passed in to a function which expects int, or comparison between signed and unsigned int and this causes the old compiler to choke on it.

I have read through the documentation and -Wsign-compare should be enabled by -Wextra but in practice seems this is not the case, so I might have missed something...


The -ansi is alias for the default standard without GNU extensions. I'd suggest instead being explicit using -std=c++98 , but it should be default for g++ -ansi , so not really different.

But generally I've never seen anything that would be accepted by newer gcc and rejected by older gcc on the grounds of being invalid. I suspect any such problem is a bug in the older compiler or it's standard library. Gcc does not have warnings for things that are correct, but didn't work with older versions of it, so you don't have any other option than to test with the older version.

As for the specific issues you mention:

  • Passing double to function that expects int is not an error. It might be undefined behaviour though. -Wconversion should help.
  • Comparing signed with unsigned is also well defined, also always worked as defined and in case of equality comparisons actually makes programmers write worse code (comparing unsigned variable larger than int with -1 is something else than comparing it with -1u). So I actually always compile with -Wno-sign-compare .
  • The compiler should not print warnings for headers found in directories given with -isystem instead of -I , so that should let you silence the warning for Qt headers and keep it enabled for your own code. So you should be able to use -Wconversion .


    Use lint or some other static analysis tool to check the code, in addition to compiler. On my Linux distro, apt-get install splint will get splint, maybe check if that has been packaged for your OS for easy installation.

    链接地址: http://www.djcxy.com/p/68892.html

    上一篇: 查询以检测重复的行

    下一篇: 对代码编译的方式非常迂腐