GCC LTO: Uncertain handling of -fcommon vs -fno-common during linking
0 reputation · 23 Mar 2025, 19:33 UTC
Symptom
When compiling with -flto and -fcommon, GCC often succeeds linking multiple source files that each declare a tentative global variable. Switching to -fno-common triggers duplicate symbol errors during the LTO phase, even though standard linking without LTO honours the flags consistently.
Uncertainty
The GCC manual does not explicitly state whether -fcommon should be treated as a weak symbol during LTO, and empirical tests show that the behavior appears to vary across GCC 11, 12, and 13. This leaves developers unsure whether to depend on -fcommon when building shared libraries or static binaries that use LTO.
Questions
- Is the treatment of
-fcommonas weak during LTO a documented GCC design choice, or an implementation quirk? - Do newer GCC releases (e.g., 13 and beyond) change the default handling of
-fcommonin LTO, and if so, how? - Should developers rely on
-fcommonfor tentative definitions when using LTO, or is a different strategy recommended?