GCC: Migrating to version 3.2

This article no longer applies to our system, but is kept here for historical reasons.

Advantages

What are some of the advantages to GCC 3.2 (compared to GCC 2.96)

GCC 3.2 has many improvements. This summary is taken from Daniel Robbins: http://linux.oreillynet.com/pub/a/linux/2002/10/10/intro_gentoo.html, a Gentoo Linux developer:

"...With gcc 3.2 come a host of performance improvements, including full support the for [sic] Athlon, Athlon XP, Pentium III, Pentium 4 and PowerPC G4 processors. This also includes support for the MMX, SSE SSE2, 3DNow! and AltiVec instruction sets. The gcc-3.2 also produces significantly tighter code than the gcc 2.95.x series and even the 3.1 series of compilers..."

GCC 3.2 follows ANSI standards much more closely than previous versions of GCC.

In January 2003, the CS department Linux workstations started using GCC 3.2 as the default compiler. GCC 2.96 will still be available but we recommend using 3.2.

Solutions to common problems

The following are several common problems encountered when transitioning from GCC 2.96 to 3.2. If you are having a problem that isn't included here, and it is something that definitely worked under 2.96 but not under 3.2, please contact us at system@cs.byu.edu

I'm having problems with my #include statements

GCC 3.2 does not require/allow you to have a '.h' in standard C++ header files. A few examples are iostream, ostream, strstring, string, and any STL code. You still use '.h' for non-standard files which you include.

2.96 Code:

#include <iostream.h>
#include <string.h>
#include "myclass.h"

3.2 Code:

#include <iostream>
#include <string>
#include "myclass.h"

Unrecognized Symbols

I get an error about unrecognized symbols with cout, eol, cin, string, vector, etc.

Unlike GCC 2.96, version 3.2 actually requires you to write correct C++ code. One example is the use of namespaces to define scope. For example, if you are having unrecognized symbol errors with cout, you could do the following:

std::cout << "Hello, world";

This basically tells it where to look for cout; std is a namespace used by common C++ facilities. If you don't want to put std:: in front of every cout you could add the following line after your #includes:

using std::cout;

Now it knows where to get cout from. The following would set the namespace for everything in std (cout, eol, cin, etc):

``using namespace std;``

Here is a nice explanation of namespaces with example code: http://www.mvps.org/windev/cpp/nspaces.html

string::compare

Has something changed with string::compare?

In a nutshell, yes. The syntax is a little different.

2.96:

string::compare(string,start,end);

3.2:

string::compare(start,end,string);

Compatibility

I use GCC 2.96 at home and 3.2 at school (or vice-versa), how can I write code that is compatible with both?

Try something like the following:

#if (__GNUC__ >= 3)
                // Put your 3.2 code here
#else
                // Put your 2.96 code here
#endif

Changes in STL Constructs

Some of the STL Constructs I'm using don't work anymore.

hash_set, hash_map, and their related classes are not officially part of the STL, but are an extension proposed by SGI. The following changes apply to these constructs:

  1. The location of the include files has changed. So #include <hash_set.h> becomes #include <ext/hash_set>.

  2. The namespace is different, as well. It has changed to __gnu_cxx. Therefore: struct hash<yourClass> {...} becomes:

        namespace __gnu_cxx {
            struct hash<yourClass> {...}
        }
    
    and ``hash_set<yourClass, hash<yourClass>, ...>`` either becomes
    ``__gnu_cxx::hash_set<yourClass, __gnu_cxx::hash<yourClass>, ...>`` or you
    could add ``using namespace __gnu_cxx;`` near the top of your file, and
    leave the hash_set definition unchanged.
    

Backwards Compatibility

I would still like to compile with GCC 2.96. How do I do that? GCC 2.96 is installed if you would prefer to continue using it. We advise using 3.2 if possible.

The following are the commands used to compile with 2.96 and 3.2

2.96:

gcc296
g++296
gcc -V 2.96
g++ -V 2.96

3.2:

gcc
g++