gmon.out isn't created when I compile with -pg flag with g++

一笑奈何 提交于 2019-12-06 18:31:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/19826735/gmon-out-isnt-created-when-i-compile-with-pg-flag-with-g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!