Chapter 2. Compiling, Linking, and Running Programs

This chapter discusses C++ compiling and linking for the MIPSpro compilers. It contains the following sections:

The C++ Command Line

This section provides an overview of the CC (1) command. For complete details about each option, see the CC man page.

The CC command invokes the compiler. The following syntax box shows the complete CC command syntax (note that cc command syntax is not indicated here).

CC [-64|-n32] [-all] [-anach] [-ansiE] [-ansiW] [-apo] [-apokeep] 
[-apolist] [-ar] [-auto_include] [-bigp_off] [-bigp_on] 
[-brief_diagnostics] [-c] [-cfront] [-common] [-D name=def] 
[-DEBUG] [-diag_error numberlist] [-diag_remark numberlist] 
[-diag_suppress numberlist] [-diag_warning numberlist] [-dollar] [-E] 
[-fb file] [-fb_create path] [-fb_opt path] [-fbgen] [-fbuse file] 
[-FE:eliminate_duplicate_inline_copies] 
[-FE:template_in_elf_section] [-float] [-float_const][-fullwarn] 
[-G num] [-g[n]] [-gslim] [-help] [-I dir] [-ignore_suffix] 
[-INLINE] [-IPA] [-J #] [-KPIC] [-L directory] [-l library] [-LANG] 
[-LIST] [-LNO] [-M] [-MDupdate filename] [-mipsn] [-MP] [-mp] 
[-mplist] [-no_auto_include] [-noinline] [-non_shared] 
[-no_prelink] [-none] [-nostinc] [-o outfile] [-O[n]] 
[-Ofast [= ipxx]] [-OPT:] [-P] [-pch] [-pedantic] [-prelink] 
[-pta] [-ptall] [-ptnone] [-ptused] [-ptv] [-quiet_prelinker] [-r] 
[-rprocessor] [-S] [-shared] [-show] [-signed] [-TARG] [-TENV] 
[-trapuv] [-U name] [-use_command] [-use_readonly_const] 
[-use_readwrite_const] [-use_suffix] [-v] [-version] 
[-W c,arg1 [,arg2...]] [-w] [-w2] [-woff all] [-woff numberlist] 
[-x lang] [-Xcpluscomm] [-Y c,path] files

In some cases, more than one option can have an effect on a single compiler feature. The following list shows some of the compiler features and the options that affect them:

  • Source preprocessing: -Dvar[ =def][,var[ =def]]..., -E, -nocpp, -P, -Uname.

  • Setting the compilation environment: -n32, -64, -mipsn, -rprocessor, -TARG:, -TENV:.

  • Optimization: -apo, -LNO:, -OPT:, -Olevel.


Note: In order to use the APO command line options, you must be licensed for the MIPSpro Auto-Parallelizing Option.

Various environment variable settings can affect your compilation. For more information on the environment variables, see the pe_environ(5) man page.

Some CC(1) command options, for example, -LNO:..., -LIST:..., -MP:... , -OPT:..., -TARG:..., and -TENV:... accept several suboptions and allow you to specify a setting for each suboption. To specify multiple suboptions, either use colons to separate each suboption or specify multiple options on the command line. For example, the following command lines are equivalent:

% CC -LIST:notes=ON:options=OFF b.c
% CC -LIST:notes=ON -LIST:options=OFF b.c

Some arguments to suboptions of this type are specified with a setting that either enables or disables the feature. To enable a feature, specify the suboption either alone or with =1, =ON, or =TRUE. To disable a feature, specify the suboption with either =0, =OFF, or =FALSE . For example, the following command lines are equivalent:

% CC -LNO:auto_dist:blocking=OFF:oinvar=FALSE a.c
% CC -LNO:auto_dist=1:blocking=0:oinvar=OFF a.c

For brevity, this manual shows only the ON or OFF settings to suboptions, but the compiler also accepts 0, 1, TRUE, and FALSE as settings.

Command Line Options

The following list summarizes the options to the CC command. For complete details, see the CC(1) man page.

-n32, -64
 

Specifies the Application Binary Interface (ABI), either -n32 or -64. Specifying -n32 generates 32-bit objects. Specifying -64 generates 64-bit objects.

-all
 

Specifies that the linker should pull in the entire archive into the shared object, not just the object files needed for the link.

-anach
 

Specifies that anachronistic C++ constructs are allowed.

-ansiE
 

Issues an error message on all code that is not standard-conforming.

-ansiW
 

Issues a warning message on all code that is not standard-conforming.

-apo, -apokeep, -apolist
 

Controls the Auto-Parallelizing Option (APO), which automatically converts sequential code into parallel code by inserting parallel directives where it is safe and beneficial to do so.


Note: These options are ignored unless you are licensed for the Auto-Parallelizing Option. For more information on this product, contact your sales representative.


-ar
 

Creates an archive instead of a shared object or executable.

-auto_include, -no_auto_include
 

Specifies that the compiler implicitly includes template definition files if such definitions are needed.

-bigp_on
 

Instructs that the compiler to enable the use of large pages within your program.

-bigp_off
 

Instructs that the compiler to disable the use of large pages within your program. This is the default for all optimization levels except -Ofast.

-brief_diagnostics
 

Issues one-line diagnostic messages.

-c
 

Disables the load step and writes the binary object file to file.o.

-cfront
 

Causes the compiler to accept constructs that were accepted by previous Cfront-based compilers.

-common
 

Relaxes the ANSI/ISO C Strict-Ref/Def-initialization model to the traditional IRIX Relaxed Ref/Def model.

-Dname[=def ][,name[ =def]]...
 

Defines name to the macro preprocessor.

-DEBUG:...
 

Controls the compiler's attempts to detect various errors (at compile time or run time) and controls how the errors are reported. For more information on the debugging options, see the debug_group(5) man page.

-diag_error numberlist
 

Treats messages with the specified numbers as errors and does not generate an object file.

-diag_remark numberlist
 

Treats messages with the specified numbers as remarks unless they are nondiscretionary errors.

-diag_suppress numberlist
 

Equivalent to specifying -woff.

-diag_warning numberlist
 

Treats messages with the specified numbers as warnings unless they are nondiscretionary errors.

-dollar
 

Allows the dollar sign as a character in C identifiers.

-E
 

Run only the source preprocessor files, without considering suffixes, and writes the result to stdout.

-fbfile
 

Specifies the feedback file to be used.

-fb_create path
 

Generates an instrumented executable program, which is suitable for producing one or more .instr files for subsequent feedback compilation.

-fb_opt path
 

Specifies the directory that contains the instrumentation output generated by compiling with -fb_create and then running your program with a training input set.

-fbgen
 

Generates an instrumented executable program.

-fbuse file
 

Specifies a .Counts file that is used to guide feedback compilation.

-FE:eliminate_duplicate_inline_copies
 

Eliminates duplicate copies of functions that are declared inline but for which an out-of-line copy must be generated.

-FE:template_in_elf_section
 

Eliminates duplicate template instantiation from an executable or DSO.

-float
 

Causes the compiler to use single-precision floating-point wherever float is specified, except in function arguments.

-float_const
 

Interprets floating point constants without precision suffixes as single-precision whenever doing so will not lose precision and the context is otherwise single-precision.

-fullwarn
 

Requests that the compiler generate comment-level messages. These messages are suppressed by default. This option can be useful during software development.

-Gnum
 

Specifies the maximum size, in bytes, of a data item that is to be accessed from the Global Pointer (GP). num must be a decimal number.

-g[n]
 

Generates debugging information and establishes a debugging level.

-gslim
 

Limits the amount of debugging information generated by the compiler for class definitions

-help
 

Lists all available options. The compiler is not invoked.

-Idir
 

Specifies a directory to be searched for #include files.

-ignore_suffix
 

Determines the language of the source file being compiled by the command used to invoke the compiler.

-INLINE:...
 

Specifies actions for the standalone inliner. For more information on the individual options in this group, see the ipa(5) man page.

-IPA[:...]
 

Controls the application of interprocedural analysis (IPA) and optimization. This includes inlining, common block array padding, constant propagation, dead function elimination, alias analysis, and other features. Specify -IPA with no arguments to invoke the interprocedural analysis phase with default options. For more information on the individual options in this group, see the ipa(5) man page.

-J[#]
 

Specifies the maximum number of concurrent compiles that the C++ prelinker is allowed to run at once.

-KPIC
 

Generates position-independent code (PIC), which is necessary for programs loaded with dynamic shared libraries. Enabled by default.

-llibrary
 

Searches the library named liblibrary .a or liblibrary.so . Libraries are searched in the order given on the command line.

-Ldirectory
 

Changes the library search algorithm for the loader.

-LANG:...
 

Controls the language option group.

-LIST:...
 

Writes an assembler listing file to file.l.

-LNO:...
 

Specifies options and transformations performed on loop nests by the Loop Nest Optimizer. For details about these options, see the lno(5) man page.

-M[]
 

Runs only the preprocessor on the named files and writes make dependencies to standard output.

-MDupdate[file]
 

Updates makefile dependencies in file.

-mipsn
 

Specifies the instruction set architecture (ISA).

-mp
 

Generates multiprocessing code for the files being compiled. This option causes the compiler to recognize all multiprocessing directives and enables all -MP:... options.

-MP:...
 

Specifies individual multiprocessing options that provide fine control over certain optimizations.

-mplist
 

Generates file.w2f.c .

-noinline
 

Suppresses expansion of inline functions.

-non_shared
 

Builds a nonshared object.

-none
 

Turns off the effects of -all for the remainder of the command line.

-nostdinc
 

Directs the system to skip the standard directory, /usr/include, when searching for #include files.

-ooutfile
 

Writes the executable file to outfile rather than to a.out. By default, the executable output file is written to a.out.

-On
 

Specifies the basic optimization level.

-Ofast[=ipxx]
 

Selects optimization that maximizes performance for a given SGI platform.

-OPT:...
 

Controls miscellaneous optimizations. These options override defaults based on the main optimization level. For details, see the opt(5) man page.

-pch
 

Uses the -LANG:pch option.

-pedantic
 

Warns that the #ident preprocessor directive is nonstandard.

-prelnk, no_prelink
 

Instructs the compiler to emit information in the object file and in an associated .ii file to help the prelinker determine which files are responsible for instantiating the various template entities referenced in a set of object files.

-P
 

Runs only the source preprocessor and puts the results for each source file in a corresponding file.i . The file.i that is generated does not contain # lines.

-pta, ptall
 

Instantiates template entities declared or referenced in the current compilation.

-ptnone,
 

No templates are instantiated.

-ptused,
 

Template entities used in this compilation will be instantiated.

-ptv,
 

Prints the name of the template entity and source file.

-quiet_prelinker,
 

Disables prelinker output in C++.

-r,
 

Creates Executable Linking Format (ELF) relocatable objects when specified with -IPA during compilation.

-rprocessor
 

Specifies the code scheduler.

-S
 

Generates an assembly file, file.s, rather than an object file (file.o).

-shared
 

Creates a shared library instead of an executable program.

-show
 

Print the passes as they execute with their arguments and their input and output files.

-signed
 

Causes values of type char to be treated as if they had type signed char.

-TARG:...
 

Cross compiling is compiling a program on one system and executing it on another. To cross compile, you can either use the -TARG: command line options to control the target architecture and machine for which code is generated or you can set the COMPILER_DEFAULTS_PATH environment variable to specify the file that contains the default processor information needed to generate executable code for the target system.

-TENV:...
 

Specifies the target environment option group. The target environment is the system upon which the executable code will be run. These options control the target environment assumed and/or produced by the compiler.

-trapuv
 

This option has been replaced by the -DEBUG:trap_uninitialized option.

-Uname
 

Undefines a variable for the source preprocessor.

-use_command
 

Uses the command name to determine which compiler to invoke for recognized source files.

-use_readonly_const
 

Puts string literals and file-level const qualified initialized variables into a .rodata section.

-use_readwrite_const
 

Puts string literals and file-level const qualified initialized variables into a readable and writable data section.

-use_suffix
 

Use the file suffix to determine which compiler to invoke.

-v
 

This option has the same functionality as -show.

-version
 

Writes compiler release version information to stdout . No input file needs to be specified when this option is used.

-w[]
 

Suppresses messages.

-w2[]
 

Counts warnings as errors.

-Wl,opt[, arg][,opt[ ,arg]]...
 

Specifies options to be passed directly to the linker.

-woffnumberlist
 

Specifies message numbers to suppress.

-woff all
 

Suppresses warning messages.

-x lang
 

Specifies that the following files are of the specified language, regardless of suffix. Valid values for lang are c, c++, f, f90, assembler, object, or none.

-Xcpluscommnumberlist
 

Applies C++ style comment rules.

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.

Compiling and Linking

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, as shown in the following examples:

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

On the right is the O32 compilation process, invoked by using -o32, as shown in the following example:

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.

  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.

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

The N32, 64, and O32 C++ Compilation Processes

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 ...

Multilanguage 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, 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.

  • elfdump: the elfdump tool lists the contents of an ELF (Executable and Linking 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 N32/64 Compiling and Performance Tuning Guide.