Editing, compiling, and linking programs with XL C/C++

The compiler phases
Editing C and C++ source files
Compiling with XL C/C++
Compiling parallelized XL C/C++ applications
XL C/C++ input files
XL C/C++ output files
Specifying compiler options
Linking XL C/C++ programs
Compiling and linking in separate steps
Dynamic and static linking

Basic C and C++ program development consists of repeating cycles of editing, compiling and linking (by default a single step combined with compiling), and running.

Prerequisite Information:
  1. Before you can use the compiler, you must first ensure that all Linux settings (for example, certain environment variables and storage limits) are correctly configured. For more information see Environment variables and XL C/C++.
  2. To learn more about writing C and C++ programs, refer to the XL C/C++ Advanced Edition V8.0 for Linux Language Reference.

The compiler phases

The typical compiler invocation command executes some or all of the following programs in sequence. For link time optimizations, some of the phases will be executed more than once during a compilation. As each program runs, the results are sent to the next step in the sequence.

  1. A preprocessor
  2. The compiler, which consists of the following phases:
    1. Front-end parsing and semantic analysis
    2. Loop transformations
    3. Interprocedural analysis
    4. Optimization
    5. Register allocation
    6. Final assembly
  3. The assembler (for .s files and for .S files after they are preprocessed)
  4. The linker ld

To see the compiler step through these phases, specify the -qphsinfo and -v compiler options when you compile your application.

Editing C and C++ source files

To create C and C++ source programs, you can use any of the available text editors, such as vi or emacs. Source programs must use a recognized filename suffix unless the configuration file or the -qsourcetype compiler option define additional non-standard filename suffixes. See XL C/C++ input files for a list of filename suffixes recognized by XL C/C++.

For a C or C++ source program to be a valid program, it must conform to the language definitions specified in the XL C/C++ Advanced Edition V8.0 for Linux Language Reference.

Compiling with XL C/C++

To compile a source program, use one of the compiler invocation commands with the syntax shown below:

Read syntax diagramSkip visual syntax diagram>>-compiler_invocation------------------------------------------>
 
   .-----------------------------------------.
   | .---------------------.                 |
   V V                     |                 |
>------+-----------------+-+----input_file---+-----------------><
       '-cmd_line_option-'
 

The compiler invocation command performs all necessary steps to compile C or C++ source files, assemble any .s and .S files , and link the object files and libraries into an executable program.

For new C or C++ application work, you should consider compiling with xlC or xlc++, or its threadsafe counterparts.

Both xlC and xlc++ will compile program source as either C or C++, but compiling C++ files with xlc may result in link or runtime errors because libraries required for C++ code are not specified when the linker is called by the C compiler. The other base compiler invocation commands exist primarily to provide explicit compilation support for different levels and extensions of the C or C++ language.

In addition to the base compiler invocation commands, XL C/C++ also provides specialized variants of many base compiler invocations. A variation on a base compiler invocation is named by attaching a suffix to the name of that invocation command. Suffix meanings for invocation variants are:

_r
Threadsafe invocation variant that supports POSIX Pthread APIs for multithreaded applications, including applications compiled with -qsmp or with source code containing OpenMP program parallelization directives.
Table 4. XL C/C++ compiler invocation commands
Base Invocation Variants on Base Invocation Description
xlC
xlc++
xlC_r
xlc++_r
Invokes the compiler so that source files are compiled as C++ language source code.
xlc xlc_r Invokes the compiler so that source files are compiled as C source code.
c99 c99_r Invokes the compiler so that source files are compiled with strict conformance to the ISO C99 standard (ISO/IEC 14882:1999).
Note:
The ISO C99 standard also specifies features in the runtime library. These features may not be supported in the runtime library currently installed on your system.
c89 c89_r Invokes the compiler so that source files are compiled with strict conformance to the ISO C89 standard (ISO/IEC 9899:1990).
cc cc_r Invokes the compiler for use with legacy C code that does not require compliance with C89 or C99.
gxlc   Invokes the compiler after translating GNU C command-line options to XL C/C++ options.
Note:
Not every GNU C option has an exact XL C/C++ equivalent.
gxlC
gxlc++
  Invokes the compiler after translating GNU C++ command-line options to XL C/C++ options.
Note:
Not every GNU C++ option has an exact XL C/C++ equivalent.

Compiling parallelized XL C/C++ applications

XL C/C++ provides threadsafe compilation invocations that you can use when compiling parallelized applications for use in multiprocessor environments.

These invocations are similar to their corresponding base compiler invocations, except that they link and bind compiled objects to threadsafe components and libraries.

Note:
Using any of these commands alone does not imply parallelization. For the compiler to recognize OpenMP directives and activate parallelization, you must also specify -qsmp compiler option. In turn, you can only specify the -qsmp option in conjunction with one of these six invocation commands. When you specify -qsmp, the driver links in the libraries specified on the smp libraries line in the active stanza of the configuration file.

XL C/C++ input files

The input files to the compiler are:

Source files (.c suffix for C language, .C .cc .cp .cpp .cxx .c++ suffixes for C++ language)
The compiler considers files with these suffixes as being C or C++ source files for compilation.

The compiler compiles source files in the order you specify on the command line. If it cannot find a specified source file, the compiler produces an error message and proceeds to the next file, if one exists.

If you have C or C++ source files that do not conform to standard C or C++ file naming conventions, you can use the -+ compiler option to instruct the compiler to treat such files as C or C++ source files. Such files, other than those with .a, .o, .so, .s, or .S filename suffixes, are compiled as C++ source files when the -+ compiler option is in effect.

Include files also contain source and often have suffixes different from those ordinarily used for C or C++ source files.

Preprocessed source files (.i suffix)
The compiler sends the preprocessed source file, filename.i, to the compiler where it is preprocessed again in the same way as a .c or .C file. Preprocessed files are useful for checking macros and preprocessor directives.
Object files (.o suffix)
After the compiler compiles the source files, it uses the ld command to link the resulting .o files, any .o files that you specify as input files, and some of the .o and .a files in the product and system library directories. The compiler can then produce a single .o object file or a single executable output file from these object files.
Assembler source files (.s and .S suffixes)
The compiler sends assembler source files to the assembler (as). The assembler sends object files to the linker at link time.
Note:
Assembler source files with a .S filename suffix are first preprocessed by the compiler, then sent to the assembler.
Shared object or library files (.so suffix)
These are object files that can be loaded and shared by multiple processes at run time. When a shared object is specified during linking, information about the object is recorded in the output file, but no code from the shared object is actually included in the output file.
Configuration files (.cfg suffix)
The contents of the configuration file determine many aspects of the compilation process, most commonly the default options for the compiler. You can use it to centralize different sets of default compiler options or to keep multiple levels of the XL C/C++ compiler present on a system.

The default configuration files are /etc/opt/ibmcmp/vac/8.0/vac.cfg and /etc/opt/ibmcmp/vac/8.0/gxlc.cfg.

Profile data files

The -qpdf1 option produces runtime profile information for use in subsequent compilations. This information is stored in one or more hidden files with names that match the pattern .*pdf*.

XL C/C++ output files

The output files that C and C++ produces are:

Executable files: a.out
By default, XL C/C++ produces an executable file that is named a.out in the current directory.
Object files: filename.o
If you specify the -c compiler option, instead of producing an executable file, the compiler produces an object file for each specified program source input file, and the assembler produces an object file for each specified assembler input file. By default, the output object files are saved to the current directory using the same file name prefixes as their corresponding source input files.
Assembler source files: filename.s
If you specify the -S compiler option, instead of producing an executable file, the XL C/C++ compiler produces an equivalent assembler source file for each specified input source file. By default, the output assembler source files are saved to the current directory using the same file name prefixes as their corresponding source input files.
Compiler listing files: filename.lst
By default, no listing is produced unless you specify one or more listing-related compiler options. The listing file is placed in the current directory, with the same file name prefix as the source file.
cpp-Preprocessed source files: filename.i
To create a preprocessed source file, specify the -P option at compilation time. The source files are preprocessed but not compiled. You can also redirect the output from the -E option to generate a preprocessed file that contains #line directives. A preprocessed source file, filename.i, is produced for each source file. By default, output preprocessor source files are saved to the current directory using the same file name prefixes as their corresponding source input files.
Make dependency files: filename.u
When the -M or -qmakedep compiler option is in effect, the compiler creates a .u file for each C or C++ source file compiled. You can use the dependency information provided by .u files to help you create make files.

Each .u file contains a line for the input file and an entry for each include file in the general form of:

file_name.o :file_name.c 
file_name.o :include_file_name

Include files are listed according to the compiler's search order rules for the #include preprocessor directive. If the include file is not found, it is not added to the .u file. Files with no include statements produce output files containing one line that lists only the input file name.

Profile data files (.*pdf*)
These are the profile-directed feedback files that the -qpdf1 compiler option produces. They are used in subsequent compilations to tune optimizations according to actual execution results.

Specifying compiler options

Compiler options perform a variety of functions, such as setting compiler characteristics, describing the object code to be produced, controlling the diagnostic messages emitted, and performing some preprocessor functions.

You can specify compiler options:

When multiple compiler options have been specified, it is possible for option conflicts and incompatibilities to occur. To resolve these conflicts in a consistent fashion, the compiler usually applies the following general priority sequence:

  1. Directive statements in your source file override command line settings
  2. Command line compiler option settings override configuration file settings
  3. Configuration file settings override default settings

Generally, if the same compiler option is specified more than once on a command line when invoking the compiler, the last option specified prevails.

Note:
The -I compiler option is a special case. The compiler searches any directories specified with -I in the vac.cfg file before it searches the directories specified with -I on the command line. The option is cumulative rather than preemptive.

Other options with cumulative behavior are -R and (lowercase L).

You can also pass compiler options to the linker, assembler, and preprocessor. See Compiler options reference for more information about compiler options and how to specify them.

Linking XL C/C++ programs

By default, you do not need to do anything special to link an XL C/C++ program. The compiler invocation commands automatically call the linker to produce an executable output file. For example, running the following command:

xlC file1.C file2.o file3.C

compiles and produces the object files file1.o and file3.o, then all object files (including file2.o) are submitted to the linker to produce one executable.

After linking, follow the instructions in Running XL C/C++ programs to execute the program.

Compiling and linking in separate steps

To produce object files that can be linked later, use the -c option.

xlc++ -c file1.C               # Produce one object file (file1.o)
xlc++ -c file2.C file3.C       # Or multiple object files (file1.o, file3.o)
xlc++ file1.o file2.o file3.o  # Link object files with appropriate libraries

It is often best to execute the linker through the compiler invocation command, because it passes some extra ld options and library names to the linker automatically.

Dynamic and static linking

XL C/C++ allows your programs to take advantage of the operating system facilities for both dynamic and static linking:

See Invoking the linkage editor for more information about linking your programs.

Also, see Constructing a library for more information about compiling and linking a library.