问题
I'm doing this in a makefile and it results on the first run creating the .gcda files in that dir; but as soon as i do the second, if find that the executable is almost as slow (and this is surely related), is still writing new files to the dir after compiled. From my understanding this shouldn't occur. Removing -fprofile-arcs
(or -lgcov
for that matter) makes the second compile complain about missing symbols. What amd i missing? I make clean
in between both of these executions btw.
I also tried some variations besides -lgcov
but i reached this one from reading the manual and realizing -fprofile-use
opens a lot of optimizations, including -fprofile-arcs
and no easy alternative i tried was working.
PROFILE_DIR=/tmp/pgo/${PN}
ifeq ($(wildcard $(PROFILE_DIR)),)
all:
CXXFLAGS += -O3 -march=native -fprofile-generate=${PROFILE_DIR} -fprofile-correction
CFLAGS += -O3 -march=native -fprofile-generate=${PROFILE_DIR} -fprofile-correction
LDFLAGS += -fprofile-arcs
$(info profile-sampling build)
else
all:
CXXFLAGS += -O3 -march=native -fprofile-use=${PROFILE_DIR} -fprofile-correction
CFLAGS += -O3 -march=native -fprofile-use=${PROFILE_DIR} -fprofile-correction
LDFLAGS += -fprofile-arcs
$(info profile-guided build)
endif
gcc version is gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
edit: the final LDFLAGS of the case with the files already is : -lpthread -ldl -lrt -fPIC -shared -Wl,--no-undefined -Wl,--version-script=link.T -fprofile-arcs
If i'm not mistaken,-Wl,--no-undefined
is sabotaging not linking gcov
or not using -fprofile-arcs
because the symbols for gcov
are somewhere in the files used even if they're no longer important and that option explicitly fails the compile on all missing symbols. So the solution might be omitting -fprofile-arcs
in the second compile and (somehow) allowing just this single library gcov
to have uninitialized symbols. I don't know how to try the second.
edit 2: unfortunately, no, it crashes at runtime without that flag and without the linking to -gcov (direct or indirect). So something in the .gcda files is forcing -lgcov and that forces the resulting executable to start writing .gcda files, but if you try to remove the -lgcov you either get a failed build or a crash at runtime. I don't understand how this is supposed to work, or why those files are not just a compile time artifact....
回答1:
This was caused by 'make clean' not removing all created .o files but a outdated partial hardcoded list. The second compilation 'reused' some .o files from the first (instrumented) compilation, and thus required the -lgcov LDFLAG which naturally started the second compilation to profile again.
来源:https://stackoverflow.com/questions/61731341/how-can-i-get-gcc-profile-guided-optimizations-to-stop-writing-files-after-being