vengy
vengy

Reputation: 2257

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?

Upvotes: 1

Views: 1234

Answers (1)

KamilCuk
KamilCuk

Reputation: 142005

is link time optimization (-flto) as optimized as whole program optimization (-fwhole-program)?

No. These are different options with different meaning.

a highly optimized

Use LTO, then PGO.

if multiple C files should be individually compiled or instead combined into a single monolithic C file, then compiled?

LTO exists exactly just for that, so that compiling multiple C files individually is the same as one C file.

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

No.

Upvotes: 0

Related Questions