

Compiling with GCC
Q. I have a program which used to compile under gcc or g++, but
now
it generates a long list of errors. What happened?
A. 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.
- 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.
- 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;
}
- Some miscellaneous code fixes.
For more information on this topic see:
Ansi C++ Standard Header Files)
http://www.cplusplus.com/doc/tutorial/tut5-2.html (local cache)
For a list of some ANSI/ISO C++ Incompatibilities see:
http://fusshuhn.ourfamily.com/cppincomp.html (local cache)
For some notes on porting applications from older versions of
libstdc++ to libstdc++-v3 see:
http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/porting-howto.html (local cache)
|
|