I was able to get g++ -pg -g with -02 optimizations to work by removing the -l from gprof command.
So first compile and run the program. Then move gmon.out and the binary compiled with debug options into the same directory. Then run
sudo gprof binary_name gmon.out binary_name > output.txt