Next: Executing Prev: Why Up: Top

Compiling a Program for Profiling

   The first step in generating profile information for your program is
to compile and link it with profiling enabled.

   To compile a source file for profiling, specify the `-pg' option when
you run the compiler.  (This is in addition to the options you normally

   To link the program for profiling, if you use a compiler such as `cc'
to do the linking, simply specify `-pg' in addition to your usual
options.  The same option, `-pg', alters either compilation or linking
to do what is necessary for profiling.  Here are examples:

     cc -g -c myprog.c utils.c -pg
     cc -o myprog myprog.o utils.o -pg

   The `-pg' option also works with a command that both compiles and

     cc -o myprog myprog.c utils.c -g -pg

   If you run the linker `ld' directly instead of through a compiler
such as `cc', you must specify the profiling startup file
`/lib/gcrt0.o' as the first input file instead of the usual startup
file `/lib/crt0.o'.  In addition, you would probably want to specify
the profiling C library, `/usr/lib/libc_p.a', by writing `-lc_p'
instead of the usual `-lc'.  This is not absolutely necessary, but
doing this gives you number-of-calls information for standard library
functions such as `read' and `open'.  For example:

     ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p

   If you compile only some of the modules of the program with `-pg',
you can still profile the program, but you won't get complete
information about the modules that were compiled without `-pg'.  The
only information you get for the functions in those modules is the
total time spent in them; there is no record of how many times they
were called, or from where.  This will not affect the flat profile
(except that the `calls' field for the functions will be blank), but
will greatly reduce the usefulness of the call graph.

automatically generated by info2www