Chapter 2. Compiling, Linking, and Running C++ Programs

This chapter contains the following major sections:

Compiling and Linking

This section discusses Silicon Graphics C++ compiling and linking.

Translators and Drivers

Programs called drivers invoke the major components of the compiler system. Those components, their functions, and their place in the compilation process are discussed in the following sections. The CC(1) command invokes the driver that controls compilation of your C++ source files. The syntax is as follows:

CC [options] filename.C [options] [filename2.C ...] 

where:

CC 

invokes the various processing phases that translate, compile, optimize, assemble, and compile-time link the program.

options 

represents the driver options, which give instructions to the processing phases. Options can appear anywhere in the command line. The options interpreted by CC are discussed in “Translator Options” in this chapter.

filename.C 

is the name of the file that contains the C++ source statements. The filename must end with one of the following acceptable suffixes: .C, .c++, .c, .cc, .cpp, .CPP, .cxx or .CXX.

Compilation

The compilation process shown in Figure 2-1 is that of the C++ source file foo.C, as it would be compiled by this command line:

CC –o foo foo.C

Figure 2-1. The Compilation Process


The following steps further describe the stages of compilation:

  1. You invoke CC on the source file, which ends with the suffix .C. The other acceptable suffixes are .C, .c++, .c, .cc, .cpp, .CPP, .cxx or .CXX.

  2. The source file then passes through the C++ preprocessor, which is built into the C++ front end (fecc, edgpcfe).

  3. The complete source is then processed by the C++ front end (fecc or edgcpfe), which produces an intermediate representative from a syntactic and semantic analysis of the source.

    This stage may also produce a prelink (.ii) file, which contains information about template instantiations.

  4. The back end (be in -64 mode) generates optimized object code (foo.o).

  5. If you want to stop the compilation at this phase, and produce object code suitable for later linking, use the following command:

    CC –c foo.c
    

    The object file foo.o is the result.

  6. edg_prelink processes the .ii files associated with the objects that will be linked together. It then recompiles sources to force template instantiation.

  7. The object files are sent to the linker ld(1), which links the standard C++ library libC.so and the standard C library libc.so to the object file foo.o and to any other object files that need to be linked to produce the executable.

  8. In -32 mode only, the executable object is sent to c++patch, which links it with global constructors and destructors. If global objects with constructors or destructors are present, the constructors need to be called at run time before function main() is called, and the destructors need to be called when the program exits. c++patch modifies the executable (a.out) to insure that these constructors and destructors get called.

Multi-Language Programs

C++ programs can be compiled and linked with programs written in other languages, such as C, Fortran, and Pascal. When your application has two or more source programs written in different languages, you should compile each program module separately with the appropriate driver. Then you can link them in a separate step. You can create objects suitable for linking by specifying the –c option. For example:

CC –c main.c++
f77 –c module1.f
cc –c module2.c

The various compilers would produce three object files: main.o, module1.o, and module2.o. Since the main module is written in C++, you should use the CC command to link. Except for C, you must explicitly specify the link libraries for the other languages with the –l options. For example, to link the C++ main module with the Fortran submodule, you would use the following command:

CC –o almostall main.o module1.o –lF77 –lI77 –lisam –lm

For further information on libraries for other languages, see the appropriate programmer's guides. See also Chapter 3, “Interfaces,” in this guide for a discussion of other topics important to writing multi-language programs.

For more information on C++ libraries, see “C++ Libraries”.

Translator Options

This section contains a summary of the most important CC translator options. See the reference page for CC(1) for a complete description of all the options. See ld(1) for a description of the linker options, and cc(1) for a description of the options interpreted by the standard C compiler. See also the information in MIPSpro Compiling, Debugging and Performance Tuning.

–E 

Run only cpp(1) on the C++ source files and send the result to standard output. This option is useful, for example, if you want to see exactly which files were included in your compilation.

–c 

Produce object files only, suppressing the link phase.

–o output 

Name the final output file output. For example,

CC –o foo foo.C

produces an executable called foo instead of the default a.out. The command is shown below.

CC –c –o bar.o foo.C

produces an object file called bar.o instead of the default foo.o.

–n (-32 mode), -show0 (-64 mode) 


Print commands generated by CC but do not execute them.

+v (-32 mode), -show (-64 mode) 


Print commands as they are executed. Short for verbose output.

+d (-32 mode), -noinline (-64 mode) 


Do not attempt inline substitution for calls to functions declared as inline.

+w (-32 mode), -fullwarn (-64 mode) 


Warn about all questionable constructs. Without the +w option, the translator issues warnings only about constructs that are almost certainly problems.

+p (-32 mode),  

Disallow all anachronistic constructs. Ordinarily, the translator warns about anachronistic constructs. Under +p (for pure), the translator will not compile code containing anachronistic constructs, such as “assignment to this.” See the USL C++ Language System Product Reference for a list of anachronisms.

In -32 mode, +p also disables cfront compatibility mode, enforcing a stricter, more standard language definition. In –64 mode, by default anachronisms are disallowed and the stricter definition is the default enforced.

-use_cfront (-32 mode) 


Use OCC instead of CC.

-cfront (-64 mode) 


Compile in cfront compatibility mode. This is the default in -32 mode. The +pp option will disable cfront compatibility mode.

–nocpp 

Skip the preprocessing stage.

Object File Tools

For information on the object file tools available to you, consult the MIPS Compiling and Performance Tuning Guide. The following tools are of special interest to the C++ programmer:

nm 

The nm tool can be used to print symbol table information for object files and archive files.

c++filt 

This C++-specific tool translates the internally coded (mangled) names generated by the C++ translator into names more easily recognized by the programmer. You can, for example, pipe the output of stdump or nm into c++filt. c++filt is installed in the directory /usr/lib/c++. For example,

nm a.out | /usr/lib/c++/c++filt

libmangle.a 

The library /usr/lib/c++/libmangle.a provides a function demangle(char *) that you can invoke from your own program to output a readable form of a mangled name. This is useful if you want to write your own tool for processing the output of nm, for example. You need to include the declaration

char * demangle(char *);

in your program, and link with the library

/usr/lib/c++/libmangle.a.

size 

The size tool prints information about the text, rdata, data, sdata, bss, and sbss sections of the specific object or archive file. The contents and format of section data are described in Chapter 10 of the Assembly Language Programming Guide.

elfdump 

The elfdump tool lists the contents (including the symbol table and header information) of an ELF-format object file. See the elfdump(1) reference page for more information.

stdump 

The stdump tool outputs a file of intermediate-code symbolic information to standard out. See the stdump(1) reference page for more information.