user15071942
user15071942

Reputation:

How external linkage of member functions occurs?

I have two Translation Units with the following codes:

//Translation Unit A
struct A {
    void f(int a) {};
};
//Translation Unit B
void f(int j);
int main()
{
    f(4);
}

In the translation unit A , struct A belongs to global scope so it have external linkage,so the member function name f also have external linkage. In the second translation unit the function f declared also have external linkage. The definition of external linkage according to C++ draft ISO N°4901(2021):

When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit.

When I run the code although I get a Link error. Why this happens? How is the linkage suppose to occur?

Upvotes: 0

Views: 136

Answers (1)

Quimby
Quimby

Reputation: 19223

There are two functions

  • A::foo which is declared in TU A and has also inline definition there.
  • ::foo in TU B which has only been declared, not defined.

foo symbol in main resolves to the latter, since the linker was not able to find its definition, it emits an error.

Upvotes: 1

Related Questions