Search code examples
visual-c++gcccompiler-constructionclangicc

Is there any benefit to passing all source files at once to a compiler?


I have read about "Whole Program Optimization" (wpo) and "Link Time Code Generation" (ltcg).

I wonder is there more inter-module-analysis going on if I pass all sources at once to the compiler from the cli (like "g++ a.cpp b.cpp")? Or is that just going to enable one of those flags?

Is there a difference between compilers for this? For instance can the intel compiler benefit from such practice while other compilers don't?


Solution

  • I wonder is there more inter-module-analysis going on if I pass all sources at once to the compiler from the cli (like "g++ a.cpp b.cpp")?

    For GCC, no, doing that does not enable any WPO, each translation unit is processed separately, in isolation. I'm 99% sure the same is true for Clang, and 90% sure it's true for most other compilers.

    With GCC, to enable inter-module optimisation you need to request it explicitly via the -flto switch, which still processes each translation unit in isolation, but additional information is written to the object file and then when they are linked together further optimisation passes are done to produce the final output.