14

I'm running on Mac OSX, version 10.8.5 (Mountain Lion). I have the following simple C++ code.

main.cpp:

#include <iostream>

int main ()
{
    std::cout << "Hello world!"<<std::endl;
    std::cout << "Goodbye world!"<<std::endl;
    return 0;
}

I'm trying to get gprof to work on my computer. As the manual suggests, I enter the following two lines into my terminal:

g++ -g -pg main.cpp -o a.out 
./a.out

However this does not generate a gmon.out file as it is supposed to. When I try typing gprof in the terminal, it says:

gprof: can't open: gmon.out (No such file or directory)

which is to be expected since gmon.out isn't there...

Any ideas on what I'm doing wrong?

EDIT: Some other things that may help:

  • My friend, who has a similar OS X version (I can ask him later to confirm), and the exact same versions of g++ and gprof, was able to use gprof successfully as I have outlined.

  • I'm using an older version of g++ but I have read online that updating to a newer version didn't help.

  • a.out works perfectly, it prints out Hello world! and Goodbye world!. I also tried this with a more complex C++ program with several classes and it still has the same problem. Everything compiles and runs normally but no gmon.out file is produced.

roschach
  • 8,390
  • 14
  • 74
  • 124
nukeguy
  • 416
  • 1
  • 7
  • 20
  • Possible duplicate of http://stackoverflow.com/questions/1101545/problem-with-gprof-on-os-x-program-is-not-of-the-host-architecture, although one of the answers claims that `gprof` now works on OS X 10.6. I can't see that you're doing anything wrong, exactly what you're doing works as you'd expect on my Linux system. – Crowman Nov 07 '13 at 03:40
  • Yeah, I have 10.8.5, I saw that question too but I figured I'd start a new thread because I have a newer version of OS X and that thread didn't seem to get anywhere. There is also a "test is not of the host architecture" issue that the other person encounters which I don't. – nukeguy Nov 07 '13 at 04:01
  • 2
    Despite what people are saying, `-pg` doesn't work on any of my systems 10.7-10.9.4. I do believe you can install your own version of gcc (separate from the Apple-provided version) and profiling will work there. – gautam Sep 10 '14 at 05:19
  • 1
    Even if *gprof* does work here, you will get no samples in your code, because, outside of I/O which *gprof* does not see anyway, the code you have compiled takes about a nanosecond. It could run thousands of times over in the time between *gprof* samples. – Mike Dunlavey Nov 09 '14 at 14:46
  • 2
    `-pg` and gprof definitely don't work for me in OSX 10.10.3. Even tried running `cc` rather than `clang` and explicitly calling `exit`. – Nick Desaulniers Jul 17 '15 at 03:37
  • @NickDesaulniers on my Mac, `/usr/bin/cc` is symlinked to `/usr/bin/clang` – Steven Lu Nov 29 '16 at 17:41

1 Answers1

2

You have to realize that OS X/MacOS does not provide GNU GCC on the system by default.

Note the output of this command:

ls -la /usr/bin/g++ /usr/bin/clang++

These executables look identical. (Actually! It looks like they are different, but somehow the filesize is identical!)

As far as I can tell, clang doesn't support the production of gprof output. As confusing as it may be, the gcc program will run clang.

I would recommend trying to use homebrew to install GCC on OS X/MacOS. You do want to be careful about how it gets installed, etc., so that you know which command corresponds to which compiler.

Steven Lu
  • 41,389
  • 58
  • 210
  • 364