Turning on whole program optimization in some stat

2019-06-16 06:49发布

问题:

In Visual Studio 2010, I have a C/C++ static library project. When I turn on the option whole program optimization in release mode, I obtain a .lib file exceeding 90 MB ! When I turn off this option the size is reduced to 24 MB. This library contains hundreds of classes generated with proto-buffer.

I'm wondering why this option increases the size ? Under which conditions we must turn it off ?

Edit : Changed MO to MB thanks chrisaycock

回答1:

Whole program optimization means things are not optimized until the link stage.

The size of a static library is not the thing to look at. When in this mode, the static library may be full of extra information needed for the final optimization/link stage. If you weren't doing whole program optimization then that information could have be thrown away after the static library was built, but when you are that information has to be kept until the end.

Look at the size of the final executable instead. (It may still increase, but it shouldn't increase by such a huge amount.)



回答2:

I'm wondering why this option increases the size?

Because you are building a static library, not an executable. Whole Program Optimization leaves a lot of the optimization until link time (rather than at compile time). Thus, your library contains unoptimized "intermediate representation" rather than assembly code.

Under which conditions we must turn it off?

For static libraries, as you've just discovered.



回答3:

Enabling whole program optimization lets linker to inline functions defined in implementation (*.cpp) files. Inlining the same function in many places can increase binary size significantly.