3

I just want to test -pg, the source file is very simple, my environment is 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");        
}

no -pg compiling is OK.

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

but -pg report error

$ 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

I have tried LDFLAGS, it is the same.

export LDFLAGS="-pg" ; gcc -o pgtest.exe pgtest.o
netawater
  • 15,214
  • 4
  • 24
  • 21
  • What about simply `gcc -o pgtest.exe pgtest.o -pg`? – chtz Apr 08 '19 at 11:27
  • @chtz it is ok, but I want to pass it by environment variables, because I use scons. – netawater Apr 09 '19 at 00:55
  • 1
    @netawater, I think you will have to check your `scons` documentation, or places where you can gets `scons` help. (Here with a `scons` tag, maybe?) I know next to nothing about `scons`. Can you add the `'-pg'` flag to your `scons` set up? Does `scons` look at environment variables? `gcc` does not look at `LDFLAGS` or anything like that. You can set `LDFLAGS` before calling `make` because `make` will look at it and pass that on to `gcc`. – SeeJayBee Apr 09 '19 at 03:10
  • @SeeJayBee thanks, I found use LINKFLAGS instead of LDFLAGS is OK for scons – netawater Oct 08 '19 at 07:47

1 Answers1

4

from the gcc info page

'-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.

so if you want to do a separate compilation and linking you need to repeate the -pg

$ gcc -c pgtest.c -pg
$ gcc -o pgtest.exe pgtest.o -pg
matzeri
  • 8,062
  • 2
  • 15
  • 16
  • thanks, but I use link command in scons, I can set set it directly, can be finished by environment variables – netawater Apr 09 '19 at 00:54