4.12 Compiling the Code

The C++ source code is compiled and linked to produce the executable program.The details of this process will vary depending on your system and compiler. The process is illustrated here for a Unix system using the public domain GNU g++ compiler.

The general syntax for invoking the g++ compiler is:

    g++  {Flags} {Paths} -o {Executable} {SourceName(s)} {LibraryName(s)}

The terms in {} brackets represent places in the command where optional arguments to the compiler may be given. Each of these terms is explained below. Note that the "{" and "}" are not part of what you actually give on the command line. They are used here to identify the parts of the command line.

The {SourceName(S)} is a list of one or more code files to be compiled. The "-o {Executable}" allows you to name the compiled executable file output by the compiler. If not given the default name of this file is "a.out".

The {Flags} in the command represent the setting of various compiler options. Typical compiler flags are:

	-g	(lower case) generate information for the debugger
	-O	(upper case letter O) turn on code optimization
	-w      (lower case) turn off all warning messages
        -Wall   turn on all warning messages
        -c      (lower case) produce only an object file; don't link.

The complete list of compiler options is extensive.

A program often uses existing routines and classes. Example of these include system routines (e.g., open a file), windowing system routines (e.g., pop up a menu), general purpose classes (e.g., a class for string manipulation), or other collections of application specific classes (e.g., for graphics).

To use existing routines and classes the compiler must know where to find the header files that define the functions and classes being used and where to find the precompiled libraries of object files. The compiler (actually the preprocessor cpp ) has a standard set of directories in the file system where it looks to find header files that are named by the "#include" directives. The compiler also has a standard set of directories in the file system where it looks to find libraries of precompiled object files that contain the code for the functions and classes defined in the header files. It is often necessary to direct the compiler to look in additional directories for header files or libraries. This information is given in the {PATHS} part of the general command shown above. Each additional directories to search is specified by a path name prefixed by either "-I" (for a directory containing header files) or "-L" (for a directory containing libraries. For example:

  -I/usr/local/include -I../../include -L/usr/local/lib -L/home/cs2704/lib

specifies two additional header file paths and two additional library file paths. The header file path "-I/usr/local/include" specifies an absolute path name (one starting at the root of the file system) while the header file path "-I../../include" specifies a path relative to the current working directory. Both of the library search paths are absolute (although relative names may also be used when appropriate).

There are two rules of good usage for organizing and naming the search paths for header files and libraries. First, directories containing header files and libraries should be named "include" and "lib", respectively. This is a common, and well understood convention. Second, the "-I" paths on the command line should be grouped together and appear before all of the "-L" paths. This is common practice.

The compiler must also be told which non-standard libraries to search. The file name for a library has a standard form:


where the ".a" suffix stands for the term "archive" and the "lib" prefix is required. The "{something}" gives the discriminating name for the library. Some common libraries are:

	libm.a		math library
	libc.a		c language library
	libg++.a	g++ library
	libXm.a		X-based motif library

A library to search is given to the compiler in the abbreviated form:


For example, to search the standard g++ class library, the term "-lg++" (without the " marks, of course) would be used. Note that the required prefix ("lib") and required suffix (".a") need not, and indeed cannot be given.

Last updated: July 28, 1995 / kafura@cs.vt.edu


©1998 Prentice-Hall, Inc.
A Simon & Schuster Company
Upper Saddle River, New Jersey 07458

Legal Statement