Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

macos

g++

gprof

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.

like image 282
nukeguy Avatar asked Nov 07 '13 02:11

nukeguy


People also ask

How do I compile C++ program using G ++ in terminal?

cpp file that you want to compile, follow the following instructions to compile and run it. Step 1 − Open a new terminal window or cmd if you are on windows. Step 3 − Now enter the following command to compile the source file using g++. In place of <name-you-want-to-give> replace it by any name like myprogram, etc.

What is the G flag in C++?

(debug) Inserting the `g' flag tells the compiler to insert more information about the source code into the executable than it normally would. This makes use of a debugger such as gdb much easier, since it will be able to refer to variable names that occur in the source code.


1 Answers

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.

like image 168
Steven Lu Avatar answered Sep 24 '22 23:09

Steven Lu