0

I am just wondering how the compiler can handle the situation where a member function is declared & defined only in a include file but this .h file is included multiple times in different source codes without complains of the linker regarding multiple definition of ....

foo_1.h:

class foo
{
public:
    auto in_include() -> void { printf( "in in_include()\n" ); }
    foo();
};

foo_1.cpp:

#include <stdio.h>
#include "foo_1.h"

foo::foo()
{
        printf( "in foo()\n" );
        in_include();
}

and finally foo_main.cpp:

#include <stdio.h>
#include "foo_1.h"

int main()
{
        foo fooObject;
}

These MCVE compiles and links fine and produces the expected output:

in foo()
in in_include()

BUT, when I add in foo_1.h this line int globar_var; then the linker complains [as I expect it]:

/tmp/ccfjJJAT.o:(.bss+0x0): multiple definition of `globar_var'
/tmp/cciob9sM.o:(.bss+0x0): first defined here

I do not consider as a duplicate because I ask why the linker does not complain. The other question is more or less asking why a function can be defined in a header file.

Peter VARGA
  • 4,780
  • 3
  • 39
  • 75
  • 2
    `auto foo() -> void` is a really convoluted way to say `void foo()` – Matti Virkkunen Jun 29 '16 at 00:45
  • 1
    Functions defined in their entirety inside a class are implicitly `inline` and thus they escape usual ODR rules. Define that function outside of the class, but still in the header and watch. – DeiDei Jun 29 '16 at 00:45
  • @AlBundy It's a different wording, but in the end you both want to know how inline member function definitions work, and the answers over there explain that. *"The other question is more or less asking why a function can be defined in a header file."* So is yours. – Baum mit Augen Jun 29 '16 at 00:52
  • @AlBundy: I agree with BaummitAugen because the answers in the other topic just tell you that in-place definitions of member functions are implicitly inline which is the reason **why** the linker does not complain. Because the standard says so. The reason why the linker does not complain is the same reason as why a function can be defined in a header file: Its just valid if (implicitly) inline. – Pixelchemist Jun 29 '16 at 00:58
  • Why do you disagree with the suggested duplicate? It looks accurate to me. It asks about a function defined inline in the class definition and why that doesn't cause trouble — which is what you are asking about too. – Jonathan Leffler Jun 29 '16 at 01:04

1 Answers1

3

Class member functions that are defined with a body within the class declaration automatically become inline functions, and therefore it's OK even if the definition is compiled multiple times. You can get the same for non-class functions by using the inline keyword.

How this is implemented in practice is up to the compiler - it could actually inline the code every time it's called (think copy and paste), or it could arrange for some linker magic I don't fully understand to happen that prevents the collision.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159