gcc optimizes code when I pass it the -O2 flag, but I\'m wondering how well it can actually do that if I compile all source files to object files and then link them
-O2
You may be looking for Link-Time Optimization (LTO), aka Whole Program Optimization.