Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning on whole program optimization in some static library increases dramatically the library size !

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

like image 560
Ghassen Hamrouni Avatar asked Dec 31 '10 08:12

Ghassen Hamrouni


3 Answers

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.)

like image 191
Leo Davidson Avatar answered Sep 21 '22 16:09

Leo Davidson


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.

like image 41
chrisaycock Avatar answered Sep 21 '22 16:09

chrisaycock


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.

like image 44
watson1180 Avatar answered Sep 22 '22 16:09

watson1180