如何在Cygwin中将-pg传递给gcc

时间:2019-04-08 04:46:00

标签: gcc cygwin gprof

我只想测试-pg,源文件非常简单,我的环境是 cygwin,

$ uname -a 
CYGWIN_NT-10.0 SHA-LPLATOW 2.8.2(0.313/5/3) 2017-07-12 10:58 x86_64 Cygwin

$ vi pgtest.c

#include <stdio.h>
void main(void){
    printf("hello, world\n");        
}

没有-pg编译是可以的。

$ gcc -c pgtest.c
$ gcc -o pgtest.exe pgtest.o

但是-pg报告错误

$ gcc -pg -c pgtest.c
$ gcc -o pgtest.exe pgtest.o
pgtest.o:pgtest.c:(.text+0x1): undefined reference to `__fentry__'
pgtest.o:pgtest.c:(.text+0x1): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__fentry__'
pgtest.o:pgtest.c:(.text+0xe): undefined reference to `_monstartup'
pgtest.o:pgtest.c:(.text+0xe): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `_monstartup'
collect2: error: ld returned 1 exit status

我尝试了LDFLAGS,它是相同的。

export LDFLAGS="-pg" ; gcc -o pgtest.exe pgtest.o

1 个答案:

答案 0 :(得分:1)

从“海湾合作委员会”信息页面

  

'-pg'
       生成额外的代码以编写适用于        分析程序“ gprof”。编译时必须使用此选项        您想要数据有关的源文件,并且在以下情况下也必须使用它        链接。

因此,如果您要进行单独的编译和链接,则需要重复-pg

$ gcc -c pgtest.c -pg
$ gcc -o pgtest.exe pgtest.o -pg