Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What GCC flags are used by major open-source projects to control undefined behavior in C and C++? [closed]

There are a handful of GCC flags that are used by major open-source projects to work-around perceived over-eagerness on the part of the compiler when handling undefined behavior, specifically in the C and C++ languages. For example:

  • -fno-strict-aliasing is a staple for those who want "traditional" behavior when type punning. For example: https://lkml.org/lkml/2003/2/26/158

  • -fwrapv is commonly used when wanting to have known behavior for signed overflow. For example: http://archives.postgresql.org/pgsql-hackers/2005-12/msg00635.php

What other ones are there? I obviously can get the full list from the GCC documentation of what flags turn what undefined behavior into what kind of implementation-defined behavior. However, I'm more interested in which options are used by major open-source projects and why?

like image 974
wjl Avatar asked Feb 01 '12 22:02

wjl


2 Answers

-D_GLIBCXX_DEBUG is not exactly a compiler flag but it enables checked STL iterators and other goodies.

std::vector<int> v(42);

v[42] = 1; //The standard says this is UB. With checked iterators you will get a run-time exception.
like image 120
zr. Avatar answered Oct 14 '22 23:10

zr.


Personally the most important one is:

  • -Werror

This forces me to fix all warnings (as warnings are really logical errors).

Then I try and turn on as many of the default warnings with:

  • -Wall
  • -Wextra

To force myself to standards comliand and thus make the code as portable as possable

  • -pedantic
  • -ansi

I like S.Myers books so I add

  • -Weffc++

But unfortunately not all the libraries I use work well with this. So I turn it off a bit.

like image 40
Martin York Avatar answered Oct 15 '22 00:10

Martin York