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.
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.
- A preprocessor
- The compiler, which consists of the following phases:
- Front-end parsing and semantic analysis
- Loop transformations
- Interprocedural analysis
- Optimization
- Register allocation
- Final assembly
- The assembler (for .s files and for .S files after they are preprocessed)
- The linker ld
To see the compiler step through these phases, specify the -qphsinfo and -v compiler options when you compile your application.
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.
To compile a source program, use one of the compiler invocation commands with the syntax shown below:
>>-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:
XL C/C++ provides threadsafe compilation invocations that you can use when compiling parallelized applications for use in multiprocessor environments.
- xlC_r
- xlc++_r
- xlc_r
- c99_r
- c89_r
- cc_r
These invocations are similar to their corresponding base compiler invocations, except that they link and bind compiled objects to threadsafe components and libraries.
The input files to the compiler are:
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.
The default configuration files are /etc/opt/ibmcmp/vac/8.0/vac.cfg and /etc/opt/ibmcmp/vac/8.0/gxlc.cfg.
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*.
The output files that C and C++ produces are:
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.
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:
- On the command line with command line compiler options.
- In the stanzas found in a compiler configuration file
- In your source code using directive statements
- Or by using any combination of these techniques.
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:
- Directive statements in your source file override command line settings
- Command line compiler option settings override configuration file settings
- 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.
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.
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.
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.
XL C/C++ allows your programs to take advantage of the operating system facilities for both dynamic and static linking:
Dynamically linked programs take up less disk space and less virtual memory if more than one program uses the routines in the shared libraries. During linking, they do not require any special precautions to avoid naming conflicts with library routines. They may perform better than statically linked programs if several programs use the same shared routines at the same time. They also allow you to upgrade the routines in the shared libraries without relinking.
Because this form of linking is the default, you need no additional options to turn it on.
Statically linked programs can be moved to and run on systems without the XL C/C++ libraries. They may perform better than dynamically linked programs if they make many calls to library routines or call many small routines. They do require some precautions in choosing names for data objects and routines in the program if you want to avoid naming conflicts with library routines. They also may not work if you compile them on one level of the operating system and run them on a different level of the operating system.
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.