How to build-in gprof support to a program built with SCons?

梦想与她 提交于 2020-01-02 02:53:07

问题


Greetings,

Here is my SConstruct file:

env = Environment()
env.Append(CCFLAGS=['-g','-pg'])
env.Program(target='program1', source= ['program1.c'])

Also here is the output of the compilation:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o program1.o -c -g -pg program1.c
gcc -o program1 program1.o
scons: done building targets.

As you can see I pass the "-pg" option to the build environment. After I build, I run the program to generate "gmon.out" but it isn't produced.

Can anyone confirm this problem? or have a solution?

Thanks.

Update:

Thanks to the advice given here, the updated working SConstruct file is as follows. The linker requires the flag, so to pass it through scons, the "LINKFLAGS" option must be used.

env = Environment()
env.Append(CCFLAGS=['-g','-pg'], LINKFLAGS=['-pg'])
env.Program(target='program1', source= ['program1.c'])

Output of compilation:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o program1.o -c -g -pg program1.c
gcc -o program1 -pg program1.o
scons: done building targets.

Note the additional "-pg" in the linking phase.


回答1:


The linker also needs the -pg option in this case. From GCC man mage:

-pg Generate extra code to write profile information suitable for the analysis program gprof. You must use this option when compiling the source files you want data about, and you must also use it when linking.

Try adding the option to LDFLAGS environment variable too.



来源:https://stackoverflow.com/questions/3484493/how-to-build-in-gprof-support-to-a-program-built-with-scons

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