views:

868

answers:

5

I'm working on a large collaborative C++ project that is both developed and run on various flavors of Linux, OS X and Windows. We compile across these platforms with GCC, Visual Studio C++ and the Intel C++ compiler. As more and more people start developing code for the project, we're starting to see weird errors in compilation and runtime that are specific to particular compilers on particular operating systems. An example of this is implicit inclusion of headers that certain OS/compiler pairs seem to find for you, accidentally overloading a function from a base class in a derived class.

My goal is to make compilation on GCC more strict and catch more errors across all platforms so that we don't keep running into these problems. Here's my list of flags that I'm thinking about trying out for GCC that I've found via Google and the GCC man pages:

  • -Wall
  • -Wextra
  • -Winit-self
  • -Wold-style-cast
  • -Woverloaded-virtual
  • -Wuninitialized
  • -Wmissing-declarations
  • -Winit-self
  • -ansi
  • -pedantic

What are the other flags that people use to make GCC (and less importantly Visual Studio C++ and the Intel C++ Compiler) obey a stricter standard of the C++ language? Be specific about which compiler and version you're talking about, as some of these might not be implemented in all versions of all compilers.

+1  A: 

You can make pedantic warnings into errors with -pedantic-errors. This will prevent developers from ignoring it. For that matter you could make all warnings into errors as well with -Werror although that can be counter productive in some cases (maybe not in yours though).

Overall, I think, as far as adhering to a strict standard goes, the -pedantic options are the most helpful.

codelogic
+1  A: 

In similar situation we gave up and moved to ACE framework, hiding the difference between platforms.

Dmitry Khalatov
+1  A: 

-pedantic-errors.

See more on gcc(1).

eed3si9n
+1  A: 

As well as -pendantic you should also provide a -std switch. If you need a stricter compile then you should know what standard you are trying to conform to. Typically for current c++ this would be -std=c++98. ( -ansi performs a similar function in C++ mode, but -std= is more explicit.)

Charles Bailey
+3  A: 

Beside the pedantic-error that everyone else suggested, IMO, it's always good to run lint as part of your compile process.

There are some tools out there:

They will save a lot of your time.

KOkon
There are a lot of commercial tools available: http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis#C.2FC.2B.2B. If you wish to invest a commercial tool you normally can get trial/evaluation versions.
Richard Corden
Thanks. It's good to know.
KOkon