DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
C and C++ compilation system

Commonly used command line options

cc and CC command line options let you

Searching for a header file

Recall that the first line of our sample C program was

   #include <stdio.h>
The format of that directive is the one you should use to include any of the standard header files that are supplied with the C and C++ compilation systems. The angle brackets (< >) tell the preprocessor to search for the header file in the standard place for header files on your system. For C, this is usually the /usr/include directory. For C++ the standard place is usually /usr/include/CC, or, if the file is not found there, /usr/include. The format is different for header files that you have stored in your own directories:
   #include "header.h"
The quotation marks (``" "'') tell the preprocessor to search for header.h first in the directory of the file containing the #include line, which will usually be your current directory, then in the standard place.

If your header file is not in the current directory, specify the path of the directory in which it is stored with the -I option to cc or CC. For instance, if you have included both stdio.h and header.h in the source file test.c:

   #include <stdio.h>
   #include "header.h"
and header.h is stored in the directory ../defs, the command
   $ cc -I../defs test.c
will direct the preprocessor to search for header.h first in the current directory, then in the directory ../defs, and finally in the standard place. It will also direct the preprocessor to search for stdio.h first in ../defs, then in the standard place. The only difference is that the current directory is searched only for header files whose name you have enclosed in quotation marks.

You can specify the -I option more than once on the cc or CC command line. The preprocessor will search the specified directories in the order they appear on the command line. You can therefore specify multiple options to cc or CC on the same command line:

   $ cc -o prog -I../defs test.c
The C++ compiler provides a mechanism which may allow you to avoid recompiling a set of header files, producing an improvement in compilation time. See ``C++ precompiled headers'' for an explanation of this mechanism.

Preparing your program for debugging

When you specify the -g option to cc or CC

   $ cc -g test.c
you arrange for the compiler to generate information about program variables and statements that will be used by the debugger debug. The information supplied to debug will allow you to use it to trace function calls, display the values of variables, and set breakpoints. See ``Using the command line interface of debug'' and ``Using the graphical interface of debug'' for further information.

Preparing your program for profiling

To use one of the profilers that are supplied with the C and C++ compilation systems, you must do two things:

  1. Compile and link your program with a profiling option:
    prof:    $ cc -qp test.c
    lprof:   $ cc -ql test.c
    fprof:   $ cc -qf test.c
    

  2. Run the profiled program:
    $ a.out
    
At the end of execution, data about your program's run-time behavior are written to a file in your current directory:
   prof:    mon.out
   lprof:   prog.cnt
where prog is the name of the profiled program. The files are inputs to the profilers. See ``Analyzing run-time behavior'' for further information.

Optimizing your program

The -O option to either cc or CC invokes the optimizer:

   $ cc -O test.c
The optimizer improves the efficiency of the assembly language code generated by the compiler. That, in turn, will speed the execution time of your object code. Use the optimizer when you have finished debugging and profiling your program.

If you know the processor your code will normally be run on, you can specify that the compilation process should generate code that is optimized specifically for that processor. For more information, see the -K option on the cc(1) and CC(1C++) manual pages.

Software administration

The -Qy option causes identification information about each invoked compilation tool to be added to the output file. This information can be useful for software administration. The information is placed in the .comment section of the resulting object file and can be printed by using the mcs command. For example:

   mcs -p object_file | grep acomp
will find the identification information associated with the compiler, acomp.
Next topic: Link editing
Previous topic: A note on command line options

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004