Compiling with GCC

I have a program which used to compile under gcc or g++, but now it generates a long list of errors. What happened?

The g++ compiler up until version 2.95 was not ANSI complaint, and had many non-standard features in the libraries. Beginning with version 3 it meets the ANSI standards and old code may need to be rewritten to be ANSI compliant. Below are some tips to help you get started.

  1. Undefined Reference errors
    If you get a long list of "undefined reference" errors when you compile your c++ code, make certain you (or your Makefile) are using g++ and not gcc to call the compiler. gcc doesn't automatically determine file type bsaed on extension. Alternatively you could add -lstdc++ to the command line to enable the gcc compiler to link in the C++ library.
  2. Header Files
    The way to include headers files from the standard library has changed. Header file names no longer maintain the .h extension, for example, iostream.h becomes iostream. Header files that come from the C language now have to be preceded by a c character in order to distinguish them from the new C++ exclusive headers files. For example, stdio.h becomes cstdio. All classes and functions defined in standard libraries are now under the std namespace instead of being global. You can either use the scope operator before all the references to standard objects, or you can use "using namespace". For example:
            //ANSI-C++ compliant hello world
            #include <iostream>
    
            int main() {
              std::cout << "Hello world in ANSI-C++\n";
              return 0;
            }
          
          -- or --
         
            //ANSI-C++ compliant hello world
            #include <iostream>
            using namespace std;
    
            int main() {
              cout << "Hello world in ANSI-C++\n";
              return 0;
            }
          
  3. Some miscellaneous code fixes. The following code will not compile:
    unsigned char c; std::cin.get(c); 
    Fix: the get() member takes a char & as its parameter, not an usigned char &.