views:

222

answers:

5

Hello there

Consider this:

class Foo{
      void func1(){
            /*func1 code*/

      }

      void func2(){
            /*func2 code*/

      }


};

Case 1: class Foo in Foo.h

Case 2: class Foo nicely seperated among Foo.h and Foo.cpp

Various other cpp files include Foo.h

My question is...Will Case 1 lead to a bigger binary?

+1  A: 

If you have the code definition in the header, the compiler might create redundant copies of each function whenever you include the .h. Those redundant copies might also trigger errors from the linker, so the practice is generally frowned upon except for inline functions.

Mark Ransom
I actually do not get the linker errors. Just concerned with the code size. Thanks for the reply
hotadvice
Offcourse, if this was C code then there would havebeen linker errors.
hotadvice
@hotadvice: No linker errors with C; the compiler would have rejected the code as invalid before invoking the linker.
Jonathan Leffler
+1  A: 

If compiler decides not to inline those functions, and generate separate body for them, these bodies will appear in each object file who uses them, but with special flag for linker - 'weak symbol'. When linker finds this flag, it will combine all symbols with that name into only one resulting symbol (maybe it will produce error message if bodies or sizes of such symbols are different)

Also RTTI info and vtables also use same scenario.

With dynamic libraries, weak symbol joining may happen at run-time, if they uses the same class.

Xeor
+1  A: 

If the functions in the header are declared as static, than yes, each module (source file) that includes that header file will store a copy of that function in the object file and the final executable will be bigger in size...

Malkocoglu
The example header does not illustrate static functions.
Jonathan Leffler
@Jonathan: Well, yes; I just thought out loud...
Malkocoglu
+2  A: 

Maybe it will, maybe it won't. It really has nothing to do with header files. What matters here is that your member functions are defined in the class definition. When member functions are defined like that, they are treated as inline functions. If the compiler decides not to actually inline any calls to these functions, there won't be any impact on code size. If the compiler decides to inline any (or all) of the calls, the answer would be "it depends". Inlining calls to small functions might result in increased code size as well as in decreased code size. This all depends on the function itself and on the compiler's capabilities (optimization capabilities specifically).

AndreyT
A: 

If the code for functions is included inline in the headers, then the compiler can use that to define the functions in the object code for each separate source file, or embed the function code directly where the functions are called. Depending on your compiler and linker and the support for C++ generally, that may leave you with larger code than you would have with the functions all defined separately. If the inline functions are small enough, you may save space by avoiding function call overhead. However, such functions have to be very small.

Jonathan Leffler