1<html lang="en"> 2<head> 3<title>Compiling - GNU gprof</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="GNU gprof"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="prev" href="Introduction.html#Introduction" title="Introduction"> 9<link rel="next" href="Executing.html#Executing" title="Executing"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<!-- 12This file documents the gprof profiler of the GNU system. 13 14Copyright (C) 1988, 1992, 1997, 1998, 1999, 2000, 2001, 2003, 152007, 2008, 2009 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 19or any later version published by the Free Software Foundation; 20with no Invariant Sections, with no Front-Cover Texts, and with no 21Back-Cover Texts. A copy of the license is included in the 22section entitled ``GNU Free Documentation License''. 23 24--> 25<meta http-equiv="Content-Style-Type" content="text/css"> 26<style type="text/css"><!-- 27 pre.display { font-family:inherit } 28 pre.format { font-family:inherit } 29 pre.smalldisplay { font-family:inherit; font-size:smaller } 30 pre.smallformat { font-family:inherit; font-size:smaller } 31 pre.smallexample { font-size:smaller } 32 pre.smalllisp { font-size:smaller } 33 span.sc { font-variant:small-caps } 34 span.roman { font-family:serif; font-weight:normal; } 35 span.sansserif { font-family:sans-serif; font-weight:normal; } 36--></style> 37<link rel="stylesheet" type="text/css" href="../cs.css"> 38</head> 39<body> 40<div class="node"> 41<a name="Compiling"></a> 42<p> 43Next: <a rel="next" accesskey="n" href="Executing.html#Executing">Executing</a>, 44Previous: <a rel="previous" accesskey="p" href="Introduction.html#Introduction">Introduction</a>, 45Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a> 46<hr> 47</div> 48 49<h2 class="chapter">2 Compiling a Program for Profiling</h2> 50 51<p>The first step in generating profile information for your program is 52to compile and link it with profiling enabled. 53 54 <p>To compile a source file for profiling, specify the ‘<samp><span class="samp">-pg</span></samp>’ option when 55you run the compiler. (This is in addition to the options you normally 56use.) 57 58 <p>To link the program for profiling, if you use a compiler such as <code>cc</code> 59to do the linking, simply specify ‘<samp><span class="samp">-pg</span></samp>’ in addition to your usual 60options. The same option, ‘<samp><span class="samp">-pg</span></samp>’, alters either compilation or linking 61to do what is necessary for profiling. Here are examples: 62 63<pre class="example"> cc -g -c myprog.c utils.c -pg 64 cc -o myprog myprog.o utils.o -pg 65</pre> 66 <p>The ‘<samp><span class="samp">-pg</span></samp>’ option also works with a command that both compiles and links: 67 68<pre class="example"> cc -o myprog myprog.c utils.c -g -pg 69</pre> 70 <p>Note: The ‘<samp><span class="samp">-pg</span></samp>’ option must be part of your compilation options 71as well as your link options. If it is not then no call-graph data 72will be gathered and when you run <code>gprof</code> you will get an error 73message like this: 74 75<pre class="example"> gprof: gmon.out file is missing call-graph data 76</pre> 77 <p>If you add the ‘<samp><span class="samp">-Q</span></samp>’ switch to suppress the printing of the call 78graph data you will still be able to see the time samples: 79 80<pre class="example"> Flat profile: 81 82 Each sample counts as 0.01 seconds. 83 % cumulative self self total 84 time seconds seconds calls Ts/call Ts/call name 85 44.12 0.07 0.07 zazLoop 86 35.29 0.14 0.06 main 87 20.59 0.17 0.04 bazMillion 88</pre> 89 <p>If you run the linker <code>ld</code> directly instead of through a compiler 90such as <code>cc</code>, you may have to specify a profiling startup file 91<samp><span class="file">gcrt0.o</span></samp> as the first input file instead of the usual startup 92file <samp><span class="file">crt0.o</span></samp>. In addition, you would probably want to 93specify the profiling C library, <samp><span class="file">libc_p.a</span></samp>, by writing 94‘<samp><span class="samp">-lc_p</span></samp>’ instead of the usual ‘<samp><span class="samp">-lc</span></samp>’. This is not absolutely 95necessary, but doing this gives you number-of-calls information for 96standard library functions such as <code>read</code> and <code>open</code>. For 97example: 98 99<pre class="example"> ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p 100</pre> 101 <p>If you are running the program on a system which supports shared 102libraries you may run into problems with the profiling support code in 103a shared library being called before that library has been fully 104initialised. This is usually detected by the program encountering a 105segmentation fault as soon as it is run. The solution is to link 106against a static version of the library containing the profiling 107support code, which for <code>gcc</code> users can be done via the 108‘<samp><span class="samp">-static</span></samp>’ or ‘<samp><span class="samp">-static-libgcc</span></samp>’ command line option. For 109example: 110 111<pre class="example"> gcc -g -pg -static-libgcc myprog.c utils.c -o myprog 112</pre> 113 <p>If you compile only some of the modules of the program with ‘<samp><span class="samp">-pg</span></samp>’, you 114can still profile the program, but you won't get complete information about 115the modules that were compiled without ‘<samp><span class="samp">-pg</span></samp>’. The only information 116you get for the functions in those modules is the total time spent in them; 117there is no record of how many times they were called, or from where. This 118will not affect the flat profile (except that the <code>calls</code> field for 119the functions will be blank), but will greatly reduce the usefulness of the 120call graph. 121 122 <p>If you wish to perform line-by-line profiling you should use the 123<code>gcov</code> tool instead of <code>gprof</code>. See that tool's manual or 124info pages for more details of how to do this. 125 126 <p>Note, older versions of <code>gcc</code> produce line-by-line profiling 127information that works with <code>gprof</code> rather than <code>gcov</code> so 128there is still support for displaying this kind of information in 129<code>gprof</code>. See <a href="Line_002dby_002dline.html#Line_002dby_002dline">Line-by-line Profiling</a>. 130 131 <p>It also worth noting that <code>gcc</code> implements a 132‘<samp><span class="samp">-finstrument-functions</span></samp>’ command line option which will insert 133calls to special user supplied instrumentation routines at the entry 134and exit of every function in their program. This can be used to 135implement an alternative profiling scheme. 136 137 </body></html> 138 139