Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. static "hell"

static "hell"

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studiohelptutorialquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dontknowitall
    wrote on last edited by
    #1

    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

    _ M 2 Replies Last reply
    0
    • D dontknowitall

      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

      _ Offline
      _ Offline
      _8086
      wrote on last edited by
      #2

      What do you want to achieve? For calling the static functions inside a class, it need not "instantiate" an object.

      ---------------------------- 286? WOWW!:-O

      D 1 Reply Last reply
      0
      • _ _8086

        What do you want to achieve? For calling the static functions inside a class, it need not "instantiate" an object.

        ---------------------------- 286? WOWW!:-O

        D Offline
        D Offline
        dontknowitall
        wrote on last edited by
        #3

        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.

        _ C 2 Replies Last reply
        0
        • D dontknowitall

          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.

          _ Offline
          _ Offline
          _8086
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • D dontknowitall

            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.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • C CPallini

              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

              D Offline
              D Offline
              dontknowitall
              wrote on last edited by
              #6

              Huh? Notice the .lib and .exe split of the original post. The .lib file IS a static library linked against the final .exe.

              C 1 Reply Last reply
              0
              • _ _8086

                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

                D Offline
                D Offline
                dontknowitall
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • D dontknowitall

                  Huh? Notice the .lib and .exe split of the original post. The .lib file IS a static library linked against the final .exe.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  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

                  D 1 Reply Last reply
                  0
                  • C CPallini

                    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

                    D Offline
                    D Offline
                    dontknowitall
                    wrote on last edited by
                    #9

                    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.

                    C 1 Reply Last reply
                    0
                    • D dontknowitall

                      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.

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • D dontknowitall

                        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

                        M Offline
                        M Offline
                        Member 754960
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups