http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55102
Bug #: 55102 Summary: The options -flto and -On do not behave as described in GCC docs Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: lto AssignedTo: unassig...@gcc.gnu.org ReportedBy: d.g.gorbac...@gmail.com > Additionally, the optimization flags used to compile > individual files are not necessarily related to those > used at link time. For instance, > > gcc -c -O0 -flto foo.c > gcc -c -O0 -flto bar.c > gcc -o myprog -flto -O3 foo.o bar.o > > This produces individual object files with unoptimized > assembler code, but the resulting binary myprog is > optimized at -O3. If, instead, the final binary is > generated without -flto, then myprog is not optimized. In fact, when you use -O3 when linking the .o files, it is already too late, the resulting binary will not be fully optimized. You need to compile the .c files with at least -O1. Thus, there is a bug either in GCC itself or in the documentation. ====== 8< ====== int foo(void) { return 0; } int main(void) { return foo(); } ====== >8 ====== $ gcc -flto -O0 -c foo.c -o foo-O0.o $ gcc -flto -O1 -c foo.c -o foo-O1.o $ gcc -flto -O0 foo-O0.o -o prog-O0-O0 $ gcc -flto -O3 foo-O0.o -o prog-O0-O3 $ gcc -flto -O0 foo-O1.o -o prog-O1-O0 $ gcc -flto -O3 foo-O1.o -o prog-O1-O3 $ nm -A prog-O0-O0 prog-O0-O3 prog-O1-O0 prog-O1-O3 | grep foo prog-O0-O0:080483f0 t foo.2337 prog-O0-O3:080483f0 t foo.2337 prog-O1-O0:080483f0 t foo.2337 GCC 4.6 gives a slight different result: $ nm -A prog-O0-O0 prog-O0-O3 prog-O1-O0 prog-O1-O3 | grep foo prog-O0-O0:08048381 t foo.1988 prog-O0-O3:08048380 t foo.1988 (GCC 4.5 crashes.)