'For gcc, is link time optimization (-flto) as optimized as whole program optimization (-fwhole-program)?

I'm looking to create a highly optimized program running under linux and was wondering if multiple C files should be individually compiled or instead combined into a single monolithic C file, then compiled? For example,

Here's a single compilation unit

gcc -o single -fwhole-program -O2 helloworld.c

helloworld.c

#include <stdio.h>

void hello(const char * name)
{
  printf("Hello, %s!\n", name);
}

int main(void)
{
  hello("world");
  return 0;
}

Here's a multiple compilation unit

 gcc -o multiple -flto -O2 hello.c world.c

hello.h

void hello(const char * name);

hello.c

#include "hello.h"

int main(void)
{
  hello("world");
  return 0;
}

world.c

#include <stdio.h>
#include "hello.h"

void hello(const char * name)
{
  printf("Hello, %s!\n", name);
}

Using these disassembly commands

objdump -S --disassemble single > single.asm

objdump -S --disassemble multiple > multiple.asm

both single.asm and multiple.asm outputs were identical:

compare

Question

Is it true to assume that using the optimized options -flto and -fwhole-program will produce the same optimized binaries?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source