C++ Compilation

Source codes for C++ programs are written as ASCII text. They must be compiled and linked to produce executable programs. The executable files are what you actually run on UNIX.

The compilation of code involves several steps:

In most modern computers, various languages, e.g. Fortran, C, and C++, have distinct syntax handlers, but often share a common translator into assembly language, and a common optimization engine.

The linking step assembles the various routines produced by the compiler during the compilation step, and resolves missing calls to either language-specific libraries or system-wide functions.

We will use the GNU C++ compiler for this course because it is (a) free and (b) more efficient than most proprietary equivalents. This compiler is invoked via the g++ command. Typing

	g++ fname.cc
will compile the file fname.cc, linked it to the default libraries and produce an executable file called by default a.out.

Several parameters may be used to modify the default in the compilation. Some of these are:

	-o out_file        specify the output file (executable, or
	                   binary) to be out_file

	-O, -O1, -O2, -O3  specify optimization level

	-c                 indicates that you only want to compile
			   the file fname.cc, in which case
	                   the output file will be fname.o

	-lname             links with the library libname.a

	-Ldirectory        directs gcc to look for libraries in
	                   directory, in addition to the
	                   standard system library path

We strongly advise that you omit optimization until you are sure a program works. Optimizers tend to change the code around internally for the sake of efficiency, and they can make it extremely difficult to find errors in a complicated program. In addition, many have been known to introduce bugs of their own...

We also maintain the following aliases on newton to facilitate your compilations:


	CC fname   will compile and link the C++ program in
		   the file fname.cc with the standard
		   C/C++ mathematics library, placing the executable
		   in fname