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

This chapter contains two major sections:

Compiling and Linking

This section discusses C++ compiling and linking for the N32, 64, and O32 compilers.

The CC(1) command (see the cc(1) man page) invokes the C++ compiler. The syntax is as follows:

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

where:

CC 

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

options 

Represents the compiler options, which give instructions to the processing phases. Options can appear anywhere in the command line.

filename 

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

CC compiles with many of the same options as cc, the C language compiler. See the cc(1) man page for more information about available options. Also of interest is the MIPSpro Compiling and Performance Tuning Guide, which discusses the following tools for optimization:

  • -O0, -O1, -O2, -O3, and -Ofast optimization options

  • Interprocedural analysis (-IPA:...) (See also the ipa(5) man page.)

  • Loop nest optimization (-LNO:...)(See also the lno(5) man page.)

  • Floating point and miscellaneous optimizations (-OPT:...) (See also the opt(5) man page.)

  • Precompiled headers

Compilation

The two compilation processes in Figure 2-1, show what transformations a C++ source file, foo.C, undergoes with the N32, 64, and O32 compilers. The MIPSpro compilation process is on the left, invoked by specifying either -n32 or -64 mode:

CC -n32 -o foo foo.C
CC -64 -o foo foo.C

On the right is the O32 compilation process, invoked with -o32:

CC -o32 -o foo foo.C

The following steps further describe the compilation stages in Figure 2-1:

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

  2. The source file passes through the C++ preprocessor, which is built into the C++ compiler.

  3. The complete source is processed using a syntactic and semantic analysis of the source to produce an intermediate representation.

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

  4. Optimized object code (foo.o) is then generated.


    Note: To stop the compilation at this stage, use the following command line:
    CC mode -c foo.C


    This command produces object code, foo.o, that is suitable for later linking.

    Figure 2-1. The N32, 64 and O32 C++ Compilation Processes

    The N32, 64 and O32 C++ Compilation Processes

  5. The compiler processes the .ii files associated with the objects that will be linked together. Then sources are recompiled to force template instantiation.

  6. The object files are sent to the linker, ld (see the ld(1) man page) 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 are needed to produce the executable.

  7. In -o32 mode, 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(), and the destructors need to be called when the program exits. c++patch modifies the executable, a.out, to ensure that these constructors and destructors get called.

Sample Command Lines

The following are some typical C++ compiler command lines:

  • To compile one program and suppress the loading phase of your compilation, use the following command line:

    CC -c program 

  • To compile with full warnings about questionable constructs, use the following command line:

    CC -fullwarn program1 program2 ...

  • To compile with warning messages off, use the following command line:

    CC -w program1 program2 ...

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 do the following:

  1. Compile each program module separately with the appropriate compiler.

  2. Link them together 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 three compilers 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. In fact, if any object file is generated by C++, it is best to use CC to link. Except for C, you must explicitly specify the link libraries for the other languages with the -l option. For example, to link the C++ main module with the Fortran submodule, you use the following command line:

CC -o almostall main.o module1.o -lftn -lm

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

Object File Tools

The following object file tools are of special interest to the C++ programmer:

nm 

This tool can print symbol table information for object and archive files.

c++filt 

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

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

libmangle.a 

The /usr/lib/c++/libmangle.a library provides a demangle(char *) function that you can invoke from your program to output a readable form of a mangled name. This is useful for writing your own tool for processing the output of nm, for example. You must declare the following in your program, and link with the library using the -lmangle option:

char * demangle(char *);

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 the Assembly Language Programming Guide.

elfdump 

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

stdump 

The stdump tool outputs a file of intermediate-code symbolic information to standard output for O32 executables only. See the stdump(1) man page for more information.

dwarfdump 

The dwarfdump tool outputs a file of intermediate-code symbolic information to standard output for -n32 and -64 compilations. See the dwarfdump(1) man page for more information.

For more complete information on the object file tools, see the MIPSpro Compiling and Performance Tuning Guide.