Saturday, December 25, 2010

g++ Compiler Options

Always strive to compile code using the highest warning level available for your compiler and eliminate warnings by modifying the code. This really helps avoid all those hours of debugging and frustration - at the end of which you discover that all the mistake you did was a silly function returning no value in some code path or forgot to add default case to switch statement!

In this article, I will present a whole list of options for g++ compiler. I have borrowed the detailed description of these options from gcc/g++ online manual. In case of any doubt, please consult relevant documentation on your system specific to your compiler version.


-D_GLIBCXX_DEBUG -g -Wall -Wextra -pedantic  -Weffc++ -Wold-style-cast -Woverloaded-virtual -Wswitch-default -Wswitch-enum -Wmissing-noreturn -Wunreachable-code –Winline


Description:


-D_GLIBCXX_DEBUG  :
This is the libstdc++ debug mode which replaces unsafe (but efficient) standard containers and iterators with semantically equivalent safe standard containers and iterators to aid in debugging user programs.

-g :
Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.

On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but will probably make other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).

Unlike most other C compilers, GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops.
Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs. 


-Wall :  Enable all warnings. Consult manual for more details.


-Wextra : Enable some extra warnings in addition to -Wall

-pedantic : Issue all the warnings demanded by strict ISO C and ISO C++

-Weffc++ :
Warn about violations of the following style guidelines from Scott Meyers' Effective C++ book:

    * Item 11: Define a copy constructor and an assignment operator for classes with dynamically allocated memory.
    * Item 12: Prefer initialization to assignment in constructors.
    * Item 14: Make destructors virtual in base classes.
    * Item 15: Have operator= return a reference to *this.
    * Item 23: Don't try to return a reference when you must return an object.

Also warn about violations of the following style guidelines from Scott Meyers' More Effective C++ book:

    * Item 6: Distinguish between prefix and postfix forms of increment and decrement operators.
    * Item 7: Never overload &&, ||, or ,.

When selecting this option, be aware that the standard library headers do not obey all of these guidelines; use grep -v to filter out those warnings.

-Wold-style-cast :
Warn if an old-style (C-style) cast to a non-void type is used within a C++ program. The new-style casts (static_cast, reinterpret_cast, and const_cast) are less vulnerable to unintended effects and much easier to search for.

-Woverloaded-virtual: Warn when a function declaration hides virtual functions from a base class.

-Wswitch-default : Warn whenever a switch statement does not have a default case.

-Wswitch-enum : Warn whenever a switch statement has an index of enumeral type and lacks a case for one or more of the named codes of that enumeration. case labels outside the enumeration range also provoke warnings when this option is used.

-Wmissing-noreturn :
Warn about functions which might be candidates for attribute noreturn. Note these are only possible candidates, not absolute ones. Care should be taken to manually verify functions actually do not ever return before adding the noreturn attribute, otherwise subtle code generation bugs could be introduced. You will not get a warning for main in hosted C environments.

-Wunreachable-code :
Warn if the compiler detects that code will never be executed.

This option is intended to warn when the compiler detects that at least a whole line of source code will never be executed, because some condition is never satisfied or because it is after a procedure that never returns.
It is possible for this option to produce a warning even though there are circumstances under which part of the affected line can be executed, so care should be taken when removing apparently-unreachable code.
For instance, when a function is inlined, a warning may mean that the line is unreachable in only one inlined copy of the function.

–Winline : Warn if a function can not be inlined and it was declared as inline. Even with this option, the compiler will not warn about failures to inline functions declared in system headers

If you have any questions/suggestions do let me know by leaving a comment. Thanks. Enjoy your time with g++!

No comments: