static "hell"
-
Let's say I have the following: ---- Start .lib ---- class A { ... }; class B { public: B(); int Func3(); static int Func1(); static int Func2(); }; class C { public: static int Func1(); }; ---- End .lib ---- ---- Start .exe ---- ... int main() { C::Func1(); } ---- End .exe ---- This is the simplest example of the problem. C::Func1() calls B::Func1(). B::Func1() executes code but DOES NOT instantiate class A, call B::Func2(), or instantiate any instance of B(). B::Func2() instantiates an instance of A and B. I have all Visual Studio 2008 compiler optimizations turned on that I can find (/GL, /Ox, /GF, /OPT:REF, /OPT:ICF, /ltcg). Yet, when using a static disassembler, I can clearly see that just because I reference one static function in a class (B::Func1()), even though the other static functions in the class are not used (B::Func2()), that code is included, which in turn makes any classes they use get included (e.g. class A), and then those functions include classes they use.... The end result is an unnecessarily _huge_ executable. As I said, this is just a simple example. As far as I can tell, this is a "bug" that has been in VS since VS.NET. The only thing I can think of to possibly solve this is to create a class for each and every static function in the code...but that will likely take weeks to complete. Any better suggestions other than that? And it would be really nice if anyone here could confirm that this is actually a bug in VS.
modified on Saturday, March 15, 2008 12:41 PM
-
Let's say I have the following: ---- Start .lib ---- class A { ... }; class B { public: B(); int Func3(); static int Func1(); static int Func2(); }; class C { public: static int Func1(); }; ---- End .lib ---- ---- Start .exe ---- ... int main() { C::Func1(); } ---- End .exe ---- This is the simplest example of the problem. C::Func1() calls B::Func1(). B::Func1() executes code but DOES NOT instantiate class A, call B::Func2(), or instantiate any instance of B(). B::Func2() instantiates an instance of A and B. I have all Visual Studio 2008 compiler optimizations turned on that I can find (/GL, /Ox, /GF, /OPT:REF, /OPT:ICF, /ltcg). Yet, when using a static disassembler, I can clearly see that just because I reference one static function in a class (B::Func1()), even though the other static functions in the class are not used (B::Func2()), that code is included, which in turn makes any classes they use get included (e.g. class A), and then those functions include classes they use.... The end result is an unnecessarily _huge_ executable. As I said, this is just a simple example. As far as I can tell, this is a "bug" that has been in VS since VS.NET. The only thing I can think of to possibly solve this is to create a class for each and every static function in the code...but that will likely take weeks to complete. Any better suggestions other than that? And it would be really nice if anyone here could confirm that this is actually a bug in VS.
modified on Saturday, March 15, 2008 12:41 PM
-
What do you want to achieve? For calling the static functions inside a class, it need not "instantiate" an object.
---------------------------- 286? WOWW!:-O
What I want to achieve is a smaller executable. And my point is that static functions I'm not referencing ANYWHERE are being included in the final executable for no good reason other than they have the 'static' keyword. I've noticed that not all functions are being included - some are getting optimized away - but not all of them.
-
What I want to achieve is a smaller executable. And my point is that static functions I'm not referencing ANYWHERE are being included in the final executable for no good reason other than they have the 'static' keyword. I've noticed that not all functions are being included - some are getting optimized away - but not all of them.
I'm hearing this for the first time!? My guess is that no matter you make a function static or not, it will be included in the exe. If you want to avoid those functions, group all those functions and put them into a dll. If anything's need , you may load the dll at run time and call the functions. That's the only way! If you are damn sure about your concept, provide me a link here.
---------------------------- 286? WOWW!:-O
-
What I want to achieve is a smaller executable. And my point is that static functions I'm not referencing ANYWHERE are being included in the final executable for no good reason other than they have the 'static' keyword. I've noticed that not all functions are being included - some are getting optimized away - but not all of them.
What about a static library?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
What about a static library?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeHuh? Notice the .lib and .exe split of the original post. The .lib file IS a static library linked against the final .exe.
-
I'm hearing this for the first time!? My guess is that no matter you make a function static or not, it will be included in the exe. If you want to avoid those functions, group all those functions and put them into a dll. If anything's need , you may load the dll at run time and call the functions. That's the only way! If you are damn sure about your concept, provide me a link here.
---------------------------- 286? WOWW!:-O
I'm "damn sure". And don't "guess" - know. And I can't make it a DLL (if I did, the DLL and the EXE would both be huge, which is the opposite of my main goal). It is a static library linked against the final EXE as demonstrated by my original post. Only people who have experienced the problem I've got should be replying - really large libraries used across multiple solutions and projects that are including 'static' functions that are never referenced. You apparently don't fall into that category. Also, I just discovered a set of static functions in the final EXE that are in a class that is completely unrelated to the program itself (NT Service related functions - and the program is not a NT Service nor makes any calls into the class that has those support functions).
-
Huh? Notice the .lib and .exe split of the original post. The .lib file IS a static library linked against the final .exe.
Well, you are right. I made also a test and the result, on Debug built, confirm your original post. On the other hand, on a Release build, the result, as far as I can understand, appears different. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Well, you are right. I made also a test and the result, on Debug built, confirm your original post. On the other hand, on a Release build, the result, as far as I can understand, appears different. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeWell, of course. Debug builds include everything (even unused code). In order to see what I'm talking about, it has to be a Release build with program database generation (/Zi, adds about 32 extra bytes to the final EXE but necessary for various things) and you need to be using a static code disassembler (e.g. IDA Pro). In other words, you need to set up your dev. environment so you can reverse-engineer the EXE (binary file) to see what I'm talking about. BTW, I compiled the simplest EXE possible (console program, displays "Hello") in Release mode and it generates a 160K file with the library, a 56K file without. It should be about 60K. There is 100K of unnecessary code being included.
-
Well, of course. Debug builds include everything (even unused code). In order to see what I'm talking about, it has to be a Release build with program database generation (/Zi, adds about 32 extra bytes to the final EXE but necessary for various things) and you need to be using a static code disassembler (e.g. IDA Pro). In other words, you need to set up your dev. environment so you can reverse-engineer the EXE (binary file) to see what I'm talking about. BTW, I compiled the simplest EXE possible (console program, displays "Hello") in Release mode and it generates a 160K file with the library, a 56K file without. It should be about 60K. There is 100K of unnecessary code being included.
Did you try with a pure
C
library? Anyway the linker behaviour you discovered is, IMHO, really disappointing. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Let's say I have the following: ---- Start .lib ---- class A { ... }; class B { public: B(); int Func3(); static int Func1(); static int Func2(); }; class C { public: static int Func1(); }; ---- End .lib ---- ---- Start .exe ---- ... int main() { C::Func1(); } ---- End .exe ---- This is the simplest example of the problem. C::Func1() calls B::Func1(). B::Func1() executes code but DOES NOT instantiate class A, call B::Func2(), or instantiate any instance of B(). B::Func2() instantiates an instance of A and B. I have all Visual Studio 2008 compiler optimizations turned on that I can find (/GL, /Ox, /GF, /OPT:REF, /OPT:ICF, /ltcg). Yet, when using a static disassembler, I can clearly see that just because I reference one static function in a class (B::Func1()), even though the other static functions in the class are not used (B::Func2()), that code is included, which in turn makes any classes they use get included (e.g. class A), and then those functions include classes they use.... The end result is an unnecessarily _huge_ executable. As I said, this is just a simple example. As far as I can tell, this is a "bug" that has been in VS since VS.NET. The only thing I can think of to possibly solve this is to create a class for each and every static function in the code...but that will likely take weeks to complete. Any better suggestions other than that? And it would be really nice if anyone here could confirm that this is actually a bug in VS.
modified on Saturday, March 15, 2008 12:41 PM
This isn't a bug. This is physical coupling. The library modules are mixing the different classes together. In this case, the module that contains the static class function C::Func1() also contains the definition of B::Func2(). That function will drag in all of classes A and B. If you want the library to generate a small footprint, you need to minimize the physical coupling. Separate the classes into distinct modules. In this case you can simply create a separate file to hold C::Func1(). Have the linker generate the map file and check the symbols and modules being included. You can get incremental improvements here so you don't have to do everything at once.