Removing an inline function call results in an unresolved external character error

I tried to prove that you need a function definition inlinein all TUs that use it. However, the following code compiles well (MSVS 2010):

inc.h

inline void foo();

test.cpp

#include "inc.h"

void x();
int main()
{
    foo();  // <--- compilation fails if I remove this call
    x();
    return 0;
}

void foo()
{
}

test2.cpp

#include "inc.h"

void x()
{
    foo();
}

Note that function calls exist to prevent optimization. It compiles, although it is foodeclared inlineand defined only in test.cpp, but is also used in test2.cpp.

If I comment on the call fooin main(), I get the expected error.

"void __cdecl foo (void)" (? foo @@ YAXXZ) referenced by the function "void __cdecl x (void)" (? x @@ YAXXZ) fatal error LNK1120: 1 unresolved external

foo ? , ?

extern foo inc.h ( foo main).

+4
1

3.2/3:

...An inline function shall be defined in every translation unit in which it is used.

TU, , , , , .

+2

All Articles