https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104712
ajrh at ajrh dot net changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |ajrh at ajrh dot net --- Comment #6 from ajrh at ajrh dot net --- Created attachment 52525 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52525&action=edit mention -ffunction-sections -Wl,-gc-sections in gcov manual On deeper investigation the original code had lots of unused inlined explicit template specializations, and a few of these had constants missing the 'inline' keyword. So gcc is behaving perfectly. Apologies for the misleading testcase. I understand better now: --keep-inline-functions is correctly generating lots of otherwise dead code, and of course some of it might not link. Though I only wanted to link it in order to run gcov to find and remove all the dead code, an amusing catch-22. Am I correct that a good way to fix this sometimes will be to use -ffunction-sections -Wl,-gc-sections ? E.g. in the example below: ---- extern int i; inline int f(const char *x) { return i; } int main(int argc, char *argv[]) { return !!argc; } ---- gcc -o x --coverage x.cpp && {x; gcov x} File 'x.cpp' Lines executed:100.00% of 2 gcc -o x --coverage -fkeep-inline-functions x.cpp && {x; gcov x} x.cpp:(.text._Z1fPKc[_Z1fPKc]+0x1c): undefined reference to `i' g++ -o x --coverage -fkeep-inline-functions -ffunction-sections -Wl,-gc-sections x.cpp && {x; gcov x} File 'x.cpp' Lines executed:50.00% of 4 ---- If that's right it might be useful to mention as a hint in the manual. Attached a texi patch if so. Thank you all for the help.