I'm trying to use gprof to profile a c++ application I've written but I can't figure out for the life of me how to download and install it. I've googled everything I can think of and can't even find a download link. Someone please help!
3 Answers
It seems there are two components to gprof. One is a part of the GCC compiler itself, invoked by using the -pg argument. The other is the gprof command, which apparently is part of GNU binutils. I'll leave it to you to figure out how to install GCC and binutils on OSX...

- 3,931
- 2
- 33
- 46
-
4It seems that gprof does not understand Mach-O executables and therefore cannot be used to profile OS X programs. – Desty Jan 21 '15 at 18:21
-
3@Mohan just do this: `mv $(type gcc) $(type gcc)-apple && brew install gcc && brew install binutils`. – thepiercingarrow Jul 01 '16 at 04:31
I did not find a MacOS solution for gprof
and gcov
did not work for me, but gperftools
(Google Performance Tools) do work. Here is how to install them on MacOS:
brew install google-perftools graphviz ghostscript gv
brew link --overwrite ghostscript
Next, run the profiler on a program:
CPUPROFILE=program_name.prof DYLD_INSERT_LIBRARIES=/usr/local/Cellar/gperftools/2.6.3/lib/libprofiler.dylib ./program_name
pprof --pdf program_name program_name.prof > program_name.pdf
You can find more options for gperftools
here. Finally, open the program_name.pdf
file in a PDF viewer such as Preview to enjoy the fancy graphviz
output.
Obviously, running the profiler on a program can be automated very easily with a Bash script, as there only is the one program_name
parameter and the shared library location is constant. Here is an example script called profile.sh
that does exactly that, but includes compiling and adds a second variable so that you can compile .cpp
files with a different name:
#!/bin/bash
g++-7 -fopenmp -O3 -o $1 $2.cpp
CPUPROFILE=$1.prof DYLD_INSERT_LIBRARIES=/usr/local/Cellar/gperftools/2.6.3/lib/libprofiler.dylib ./$1
pprof --pdf $1 $1.prof > $1.pdf
echo "Profiling results: $1.pdf"
Next, modify the permissions so it can run from any folder:
chmod +x profile.sh
The script can be invoked from the command line as follows, automating the full process:
./profile.sh program_name cpp_name
You may want to separate the compilation and profiling steps, which is easy enough to do by removing the g++-7
line in the shell script.

- 6,027
- 2
- 46
- 33
-
-
-
2
-
2these steps don't work for me, see https://github.com/gperftools/gperftools/issues/1292; `pprof` fails with: error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool-classic: can't open file: /usr/lib/liboah.dylib (No such file or directory) – timotheecour Aug 07 '21 at 19:56
Since gprof does not work on OS X at the moment, use Google Performance Tools, now known as gperftools.
Also gcov works "out of the box" if you have gcc installed.
$ gcc -fprofile-arcs -ftest-coverage your_program.c
$ a.out
$ gcov your_program.c

- 14,394
- 4
- 46
- 40

- 4,709
- 2
- 20
- 21
-
1cohadar, did you check that google perf tools (pprof + libprofiler) works on OSX? gcov is not profiling tool, it is coverage tool... – osgx Jun 29 '16 at 17:56
-
If `your_program.c` exists in a different folder of running `gcov`, it will not work. You must pass `your_program.gcno` to the `gcov` to work. – Hamid Rouhani Oct 14 '16 at 13:31