1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- This file documents the gprof profiler of the GNU system.
4
5Copyright (C) 1988-2021 Free Software Foundation, Inc.
6
7Permission is granted to copy, distribute and/or modify this document
8under the terms of the GNU Free Documentation License, Version 1.3
9or any later version published by the Free Software Foundation;
10with no Invariant Sections, with no Front-Cover Texts, and with no
11Back-Cover Texts.  A copy of the license is included in the
12section entitled "GNU Free Documentation License".
13 -->
14<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
15<head>
16<title>GNU gprof: Compiling</title>
17
18<meta name="description" content="GNU gprof: Compiling">
19<meta name="keywords" content="GNU gprof: Compiling">
20<meta name="resource-type" content="document">
21<meta name="distribution" content="global">
22<meta name="Generator" content="makeinfo">
23<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
24<link href="index.html#Top" rel="start" title="Top">
25<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
26<link href="index.html#Top" rel="up" title="Top">
27<link href="Executing.html#Executing" rel="next" title="Executing">
28<link href="Introduction.html#Introduction" rel="previous" title="Introduction">
29<style type="text/css">
30<!--
31a.summary-letter {text-decoration: none}
32blockquote.smallquotation {font-size: smaller}
33div.display {margin-left: 3.2em}
34div.example {margin-left: 3.2em}
35div.indentedblock {margin-left: 3.2em}
36div.lisp {margin-left: 3.2em}
37div.smalldisplay {margin-left: 3.2em}
38div.smallexample {margin-left: 3.2em}
39div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
40div.smalllisp {margin-left: 3.2em}
41kbd {font-style:oblique}
42pre.display {font-family: inherit}
43pre.format {font-family: inherit}
44pre.menu-comment {font-family: serif}
45pre.menu-preformatted {font-family: serif}
46pre.smalldisplay {font-family: inherit; font-size: smaller}
47pre.smallexample {font-size: smaller}
48pre.smallformat {font-family: inherit; font-size: smaller}
49pre.smalllisp {font-size: smaller}
50span.nocodebreak {white-space:nowrap}
51span.nolinebreak {white-space:nowrap}
52span.roman {font-family:serif; font-weight:normal}
53span.sansserif {font-family:sans-serif; font-weight:normal}
54ul.no-bullet {list-style: none}
55-->
56</style>
57
58
59</head>
60
61<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
62<a name="Compiling"></a>
63<div class="header">
64<p>
65Next: <a href="Executing.html#Executing" accesskey="n" rel="next">Executing</a>, Previous: <a href="Introduction.html#Introduction" accesskey="p" rel="previous">Introduction</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
66</div>
67<hr>
68<a name="Compiling-a-Program-for-Profiling"></a>
69<h2 class="chapter">2 Compiling a Program for Profiling</h2>
70
71<p>The first step in generating profile information for your program is
72to compile and link it with profiling enabled.
73</p>
74<p>To compile a source file for profiling, specify the &lsquo;<samp>-pg</samp>&rsquo; option when
75you run the compiler.  (This is in addition to the options you normally
76use.)
77</p>
78<p>To link the program for profiling, if you use a compiler such as <code>cc</code>
79to do the linking, simply specify &lsquo;<samp>-pg</samp>&rsquo; in addition to your usual
80options.  The same option, &lsquo;<samp>-pg</samp>&rsquo;, alters either compilation or linking
81to do what is necessary for profiling.  Here are examples:
82</p>
83<div class="example">
84<pre class="example">cc -g -c myprog.c utils.c -pg
85cc -o myprog myprog.o utils.o -pg
86</pre></div>
87
88<p>The &lsquo;<samp>-pg</samp>&rsquo; option also works with a command that both compiles and links:
89</p>
90<div class="example">
91<pre class="example">cc -o myprog myprog.c utils.c -g -pg
92</pre></div>
93
94<p>Note: The &lsquo;<samp>-pg</samp>&rsquo; option must be part of your compilation options
95as well as your link options.  If it is not then no call-graph data
96will be gathered and when you run <code>gprof</code> you will get an error
97message like this:
98</p>
99<div class="example">
100<pre class="example">gprof: gmon.out file is missing call-graph data
101</pre></div>
102
103<p>If you add the &lsquo;<samp>-Q</samp>&rsquo; switch to suppress the printing of the call
104graph data you will still be able to see the time samples:
105</p>
106<div class="example">
107<pre class="example">Flat profile:
108
109Each sample counts as 0.01 seconds.
110  %   cumulative   self              self     total
111 time   seconds   seconds    calls  Ts/call  Ts/call  name
112 44.12      0.07     0.07                             zazLoop
113 35.29      0.14     0.06                             main
114 20.59      0.17     0.04                             bazMillion
115</pre></div>
116
117<p>If you run the linker <code>ld</code> directly instead of through a compiler
118such as <code>cc</code>, you may have to specify a profiling startup file
119<samp>gcrt0.o</samp> as the first input file instead of the usual startup
120file <samp>crt0.o</samp>.  In addition, you would probably want to
121specify the profiling C library, <samp>libc_p.a</samp>, by writing
122&lsquo;<samp>-lc_p</samp>&rsquo; instead of the usual &lsquo;<samp>-lc</samp>&rsquo;.  This is not absolutely
123necessary, but doing this gives you number-of-calls information for
124standard library functions such as <code>read</code> and <code>open</code>.  For
125example:
126</p>
127<div class="example">
128<pre class="example">ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p
129</pre></div>
130
131<p>If you are running the program on a system which supports shared
132libraries you may run into problems with the profiling support code in
133a shared library being called before that library has been fully
134initialised.  This is usually detected by the program encountering a
135segmentation fault as soon as it is run.  The solution is to link
136against a static version of the library containing the profiling
137support code, which for <code>gcc</code> users can be done via the
138&lsquo;<samp>-static</samp>&rsquo; or &lsquo;<samp>-static-libgcc</samp>&rsquo; command-line option.  For
139example:
140</p>
141<div class="example">
142<pre class="example">gcc -g -pg -static-libgcc myprog.c utils.c -o myprog
143</pre></div>
144
145<p>If you compile only some of the modules of the program with &lsquo;<samp>-pg</samp>&rsquo;, you
146can still profile the program, but you won&rsquo;t get complete information about
147the modules that were compiled without &lsquo;<samp>-pg</samp>&rsquo;.  The only information
148you get for the functions in those modules is the total time spent in them;
149there is no record of how many times they were called, or from where.  This
150will not affect the flat profile (except that the <code>calls</code> field for
151the functions will be blank), but will greatly reduce the usefulness of the
152call graph.
153</p>
154<p>If you wish to perform line-by-line profiling you should use the
155<code>gcov</code> tool instead of <code>gprof</code>.  See that tool&rsquo;s manual or
156info pages for more details of how to do this.
157</p>
158<p>Note, older versions of <code>gcc</code> produce line-by-line profiling
159information that works with <code>gprof</code> rather than <code>gcov</code> so
160there is still support for displaying this kind of information in
161<code>gprof</code>. See <a href="Line_002dby_002dline.html#Line_002dby_002dline">Line-by-line Profiling</a>.
162</p>
163<p>It also worth noting that <code>gcc</code> implements a
164&lsquo;<samp>-finstrument-functions</samp>&rsquo; command-line option which will insert
165calls to special user supplied instrumentation routines at the entry
166and exit of every function in their program.  This can be used to
167implement an alternative profiling scheme.
168</p>
169<hr>
170<div class="header">
171<p>
172Next: <a href="Executing.html#Executing" accesskey="n" rel="next">Executing</a>, Previous: <a href="Introduction.html#Introduction" accesskey="p" rel="previous">Introduction</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
173</div>
174
175
176
177</body>
178</html>
179