对于 gcc,link 时间优化 (-flto) 是否与整个程序优化 (-fwhole-program) 一样优化?

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

我想在 linux 下创建一个高度优化的程序 运行 并且想知道是否应该单独编译多个 C 文件,或者将其组合成一个单一的 C 文件,然后再编译?例如,

这里有一个编译单元

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;
}

这里有一个多个编译单元

 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);
}

使用这些反汇编命令

objdump -S --disassemble single > single.asm

objdump -S --disassemble multiple > multiple.asm

single.asm 和 multiple.asm 输出相同:

问题

是否假设使用优化选项 -flto-fwhole-program 会产生相同的优化二进制文件?

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

没有。这些是具有不同含义的不同选项。

a highly optimized

使用 LTO,然后使用 PGO。

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

LTO 正是为此而存在,因此单独编译多个 C 文件与编译一个 C 文件是一样的。

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

没有