Porting to GCC 4.6

The GCC 4.6 release series differs from previous GCC releases in more than the usual list of changes. Some of these are a result of bug fixing, and some old behaviors have been intentionally changed in order to support new standards, or relaxed in standards-conforming ways to facilitate compilation or runtime performance. Some of these changes are not visible to the naked eye and will not cause problems when updating from older versions.

However, some of these changes are visible, and can cause grief to users porting to GCC 4.6. This document is an effort to identify major issues and provide clear solutions in a quick and easily searched manner. Additions and suggestions for improvement are welcome.

C language issues

New warnings for unused variables and parameters

The behavior of -Wall has changed and now includes the new warning flags -Wunused-but-set-variable and (with -Wall -Wextra) -Wunused-but-set-parameter. This may result in new warnings in code that compiled cleanly with previous versions of GCC.

For example,

  void fn (void)
  {
    int foo;
    foo = bar ();  /* foo is never used.  */
  }

Gives the following diagnostic:

warning: variable "foo" set but not used [-Wunused-but-set-variable]

Although these warnings will not result in compilation failure, often -Wall is used in conjunction with -Werror and as a result, new warnings are turned into new errors.

To fix, first see if the unused variable or parameter can be removed without changing the result or logic of the surrounding code. If not, annotate it with __attribute__((__unused__)).

As a workaround, add -Wno-error=unused-but-set-variable or -Wno-error=unused-but-set-parameter.

C++ language issues

Header dependency changes

Many of the standard C++ library include files have been edited to no longer include <cstddef> to get namespace std -scoped versions of size_t and ptrdiff_t.

As such, C++ programs that used the macros NULL or offsetof without including <cstddef> will no longer compile. The diagnostic produced is similar to:

error: 'ptrdiff_t' does not name a type
error: 'size_t' has not been declared
error: 'NULL' was not declared in this scope
error: there are no arguments to 'offsetof' that depend on a template
parameter, so a declaration of 'offsetof' must be available

Fixing this issue is easy: just include <cstddef>.

Links

Jakub Jelinek, GCC 4.6 related common package rebuild failures (was Re: mass rebuild status)

Matthias Klose, prepare to fix build failures with new GCC versions