Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making GCC and other C++ compilers very strict

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.

like image 645
James Thompson Avatar asked Jan 29 '09 07:01

James Thompson


People also ask

Is gcc the best C compiler?

Though there are many compilers available for C, GCC stands out to be one of the best as of now. The winner declaration here lies based on durability, optimization, speed, and code/error/syntax checks. Through this, we can clearly understand that the Compiler is an important pillar to the programming languages.

Is gcc the only C compiler?

GCC, formerly for "GNU C Compiler", has grown over times to support many languages such as C ( gcc ), C++ ( g++ ), Objective-C, Objective-C++, Java ( gcj ), Fortran ( gfortran ), Ada ( gnat ), Go ( gccgo ), OpenMP, Cilk Plus, and OpenAcc. It is now referred to as "GNU Compiler Collection".

What does gcc C compiler mean?

GCC stands for “GNU Compiler Collection”. GCC is an integrated distribution of compilers for several major programming languages. These languages currently include C, C++, Objective-C, Objective-C++, Fortran, Ada, D, and Go. The abbreviation GCC has multiple meanings in common use.

What is difference between gcc & G ++ compiler?

DIFFERENCE BETWEEN g++ & gccg++ is used to compile C++ program. gcc is used to compile C program.


4 Answers

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:

  • cpplint (free)
  • gimple lint
  • coverity

They will save a lot of your time.

like image 170
KOkon Avatar answered Oct 15 '22 19:10

KOkon


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.

like image 33
codelogic Avatar answered Oct 15 '22 19:10

codelogic


Copy and paste the below line into your master CMake file. The below line comprises almost most useful compiler flags in order to test yourself stricter.

set(CMAKE_CXX_FLAGS "-O0 -fno-elide-constructors -pedantic-errors -ansi -Wextra -Wall     -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized -Wmissing-declarations    -Winit-self -std=c++98")

If you don’t use CMake, just copy the flags in double quotes and send them to your compiler.

like image 44
Validus Oculus Avatar answered Oct 15 '22 20:10

Validus Oculus


-pedantic-errors.

See more on gcc(1).

like image 37
Eugene Yokota Avatar answered Oct 15 '22 18:10

Eugene Yokota