This document introduces you to the IBM AIX XL C compiler running on the RS/6000 Unix machines. The steps to create, compile, and run a C program are discussed. In the section entitled "Compiling Your Program", alternate commands are given for calling different versions of the compiler. Utilities for debugging C/C++ programs as well as tools for code optimization are discussed.
Before you use a C or C++ compiler on the RS/6000, you may want to refer to the local document U-001, "Unix Resource List". Local documentation is listed that may be obtained from Information Technology and Communication (ITC) outside Wilson 235 or from the University Bookstore. You can view an on-line version of the manual page for the compiler by typing man xlc at a Unix prompt.
The following IBM manuals are available for reference use only from the Help Desk in Wilson 235. These manuals are also available for reference in the Unix labs (Small Building 102 and Thornton Hall E225). Personal copies may be ordered through Bob Braden, the local IBM Marketing Representative at 974-4401. Copies have also been placed in the Science and Engineering Library (Clark Hall).
|
SC09-1259 |
AIX XL C User's Guide |
|
SC09-1260 |
AIX XL C Language Reference |
|
SC23-2199 |
AIX Commands Reference for IBM RISC System/6000 |
Manuals can be viewed on-line using the InfoExplorer by entering the command:
info
and then selecting "Programming" followed by "Languages" to get a list of available manuals. The AIX C++ manuals can be accessed by invoking the InfoExplorer with the command:
info -l xlC
This link contains a README file and several examples that illustrate the use of the C/C++ compilers.
Consultants at the Help Desk in Wilson Hall are familiar with C, C++ and the Unix operating system. They can assist you in using debugging tools and help you understand any error messages you may get. You can either stop by Wilson 235, call 924-3731 or send electronic mail to unix-consult@virginia.edu.
A source file is the file that contains your program. Use an editor, e.g. JOVE or Vi, to type your program into the source file or to edit it. The filename normally has the file extension .c or .i. A file with the latter extension is the result of compiling with the -P option so that a preprocessed source file is output, but processing proceeds no further.
The commands listed below invoke versions of the XL C compiler, which then translates C source code statements into object code, sends .s files to the assembler, and links the resulting object files with object files and libraries specified on the command line in the order in which they are listed, producing a single executable file called "a.out" by default. The -o flag may be used to rename the resulting executable file. Where commands are shown, they are generally given as generic examples. In any case, you type the appropriate command and press the Return (or Enter) key as usual.
You compile a source program and/or subprograms by typing the following command:
xlc cmd_line_opts input_files
where cmd_line_opts refers to compiler options. See the XL C User's Guide, the xlc manpage, or type
xlc
with no options for information on the options you can use.
input_files are source files (.c or .i), object files (.o), or assembler files (.s)
For example, to compile a C program whose source is in source file "prog.c" you would enter the following command:
xlc prog.c
After the xlc command completes, you will see a new executable file named "a.out" in your directory.
The following examples illustrate the syntax required for some of the more commonly used options:
|
xlc -O prog.c |
compile with optimization |
|
xlc -g prog.c |
compile with debugging information included |
|
xlc -qsource prog.c |
produce a listing file prog.lst and a.out |
|
xlc -orunprog prog.c |
produce an executable named runprog instead of a.out |
|
xlc -lkey prog.c |
selects the library file libkey.a |
Several commands have been aliased on the RS/6000 to invoke the compiler in various ways, to accomodate the various flavors of C in which programs are written. The commands are as follows:
|
xlc |
ANSI C compiler |
|
cc |
Kernighan and Ritchie C compiler |
|
c89 |
ANSI C compiler |
|
bsdcc |
emulates the Berkeley C environment |
Additional options, as outlined above, may be used with these commands. The above are all actually calls to the xlc compiler.
Other C compilers are available.
|
xlC |
The AIX C++ compiler |
|
gcc |
The gnu project C compiler |
|
g++ |
The gnu project C++ compiler |
xlC can be called with the same options as xlc and can be used to compile either C or C++ source files. The command g++ is a script which calls gcc with options to recognize c++ source code. For more information on the gcc or g++ compilers, see their man pages. (man gcc, man g++)
Another useful tool for compiling C or C++ programs is the make command generator. Make is especially useful in object oriented programming. It has the ability to decide whether parts of a file need to be compiled (i.e. which source files have been modified, or have missing object files). After compiling a part of the program, it can also be told to link the object files together. An excellent source of information about makefiles can be found at the URL http://web.mit.edu/afs/athena.mit.edu/project/gnu/doc/html/make_toc.html
If you specify -c as a compiler option, XL C only compiles the source program, producing an object file whose default name is that of the program with a .o extension. Before running the program, you must invoke the linkage editor phase. Either invoke the linker using the ld command or issue the xlc command a second time without the -c option, using the desired object (.o) filenames.
For example, you may compile a subprogram "second.c" and then use it in your main program "prog.c" with the following sequence of commands:
xlc -c second.c xlc prog.c second.o
See the AIX Commands Reference Manual for further information on the linkage editor and the link-edit flags.
After your program compiles without error you may run it. Standard Unix redirection may be used with the executable "a.out" file as follows:
a.out <infile >outfile
where infile is the input filename and outfile is the output filename
Two tools are available for detecting and correcting errors in C/C++ programs. The first is the dbx symbolic debugging package which is discussed in the ITC document U-029, The dbx Debugger on the RS/6000. The on-line manual page can be viewed by typing man dbx, and the files in the ITCWeb Hints area for dbx contain additional information. In order to use the dbx debugger, compilation should be done with the -g option and no optimization.
xlc -g prog.c
The executable a.out can then be run from within the dbx environment. An X-window version of the debugger, xdbx, is also available with an on-line manual page (man xdbx). A debugger similar to dbx called gdb is available for the gcc compiler, along with an on-line manual page (man gdb).
The second debugging tool is the C/C++ program checker lint. This utility is run directly on the source code to detect semantic errors that the compiler might miss.
lint [-option, -option ...] prog.c
Further information can be accessed on-line by typing man lint.
The AIX manual chapter for diagnosing problems in a C program can be accessed through InfoExplorer by typing:
infoand successively selecting "Programming","Languages", "XL C User's Guide", and "Chapter 7".
You are advised not to use the optimization option when debugging, since optimization can move or delete lines of code and discards much of the useful symbol-table information. Use the basic xlc command until the program compiles and runs.
Once your program is debugged, code optiminization can be invoked by the -O compiler option, as shown previously. Information about this option and more general AIX C/C++ program optimization issues can be accessed through InfoExplorer by typing:
infoand successively selecting "Programming","Languages", "XL C User's Guide", and "Chapter 6".
There are three tools available on the RS/6000s that can assist you with optimization. The simplest is the timex command, which reports in seconds, the elapsed time, user time, and system execution time for a command. With specified flags, the timex command lists or summarizes process accounting data for a command (executable file) and all of its children. The on-line manual page can be accessed by typing man timex.
timex a.out
The other two tools are the Unix utilities prof and gprof, which generate execution profiles. The profile for prof breaks down the execution time to the subroutine and functional level. The profile for gprof also includes which routines call which. Manual pages are available online for both utilities (man prof, man gprof). To enable profiling, relink the programs object modules with one of the following commands, which enable prof or gprof, respectively:
xlc -p prog.c xlc -pg prog.c
After running the executable and the program completes, type the name of the profiler you selected and the information about time spent in subroutines and functions will appear. Concentrate your efforts on optimizing the code in the one or two routines where most of the time is spent.
If you are using the command bsdcc to emulate the Berkeley C environment, you will need to refer to the document "Porting 4.3 BSD Programs to AIX Version 3.1". This is available on-line in the directory /usr/lpp/bos in two forms. The file bsdport contains a version that is readable from the screen, and the file bsdport.tr is the Troff source from which a hard copy may be produced. To produce a copy, use the command:
psroff -me bsdport.tr -Pprinter
where printer is the name of the printer to be used, e.g. watt_l1
WE REALLY WANT TO KNOW...
Have you found this publication useful? Are there ways we
might improve or supplement it or other ITC publications?
Did you find this document in Web or in hard copy?
We really want your opinion. Send e-mail to: newsdesk@virginia.edu
[an error occurred while processing this directive]