使用profiling标志编译的代码不会生成gmon.out

Bra*_*ndt 9 c c++ gcc profiling gprof

我使用profiling标志(-pg)编译了一个带gcc的代码,但是当我运行程序时,没有生成gmon.out.
我编译了一个测试代码 - 实际上是来自这个问题的代码 - 看看编译标志和gprof是否正常工作,是的,它是否有效.

为了编译代码(命名xrttimetag),使用了以下行(下面我使用了-I(...)以及-L(...)隐藏其他科学库的大量路径列表):

gcc -c  -o ./xrttimetag.o  -Wall --pedantic -Wno-comment -Wno-long-long -pg -fPIC -I(...) -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE_URL="" -Dg77Fortran=1 -DgFortran=1 -DHAVE_CONNECT=1 -DHAVE_ACCEPT=1 -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_LIBM=1 -DHAVE_LIBDL=1 -DHAVE_LIBNCURSES=1 -DSIZEOF_LONG=8 xrttimetag.c

gcc -o xrttimetag xrttimetag.o      -L(...) -lswxrt -latFunctions3.3 -lcoordfits -lcoord -lephemeris -lhdinit_2.7 -lhdutils_2.7 -lape_2.8 -lcfitsio_3.37 -lreadline -lhdio_2.7 -lncurses -ldl -lm  -L/usr/lib64/gcc/x86_64-suse-linux/4.6 -L/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/lib -L/usr/lib64/gcc/x86_64-suse-linux/4.6/../../.. -L/usr/lib64/gcc/x86_64-suse-linux/4.6 -L/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/lib -L/usr/lib64/gcc/x86_64-suse-linux/4.6/../../.. -lgfortran -lm -lgcc_s -lgcc -lquadmath -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc 
Run Code Online (Sandbox Code Playgroud)

我查找了与gmon生成的二进制文件相关的符号,它们对我来说有点奇怪,因为它们未定义:

readelf -s `which xrttimetag` | egrep "gmon|mcount"
 21: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
 74: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND mcount@GLIBC_2.2.5 (2)
 41: 000000000040267c     0 FUNC    LOCAL  DEFAULT   15 call_gmon_start
 96: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
166: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND mcount@@GLIBC_2.2.5
Run Code Online (Sandbox Code Playgroud)

另一方面,我编译的测试代码:

g ++ -pg test.cpp

搜索"gmon | mcount"符号给了我:

readelf -s test | egrep "gmon|mcount"
 6: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND mcount@GLIBC_2.2.5 (3)
11: 0000000000400850    63 FUNC    GLOBAL DEFAULT   15 __gmon_start__
40: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS gmon-start.c
43: 0000000000400890     0 FUNC    LOCAL  DEFAULT   15 call_gmon_start
73: 0000000000400850    63 FUNC    GLOBAL DEFAULT   15 __gmon_start__
91: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND mcount@@GLIBC_2.2.5
Run Code Online (Sandbox Code Playgroud)

我们可以为_ test_代码定义"gmon"符号,而不是_ xrttimetag_,但我不明白为什么.我错过了什么?

谢谢.

PS:我看到问题gmon.out在用gcc -pg -g编译后没有写出来,而且这个不是重复,除非我完全误解了那个.

P.P*_*.P. 17

-pg生成可执行文件时,您没有通过.

gcc -o xrttimetag xrttimetag.o ....
Run Code Online (Sandbox Code Playgroud)

你也应该-pg在这里传递选项.如果我-pg在编译时使用但在链接时不使用,我可以重新产生问题(即gmon*调用的符号未定义).

gcc文档:

-pg

生成额外的代码以编写适用于分析程序gprof的配置文件信息.编译所需数据的源文件时必须使用此选项,并且在链接时也必须使用它.