Please consider the following code:
Test2.h:
#ifndef ABCD
#define ABCD
#ifdef __cplusplus
extern "C" {
#endif
void Foo();
#ifdef __cplusplus
}
#endif
#endif // ABCD
Test2.cpp
#include "StdAfx.h"
#include "Test2.h"
inline void Foo()
{
}
Test.cpp:
#include "stdafx.h"
#include "Test2.h"
int _tmain(int argc, _TCHAR* argv[])
{
Foo();
return 0;
}
When I compile this code I get LNK2019 error (Unresolved external symbol _Foo). I can solve it in two ways.
Assuming I want this function inline, why do I have to add extern to the declaration?
I use VS2008.
Thanks.
C++11 Standard Paragraph 3.2.3:
An inline function shall be defined in every translation unit in which it is odr-used.
You have 2 translation units, first made from Test2.cpp
...:
// ... code expanded from including "StdAfx.h"
extern "C" { void Foo(); }
inline void Foo() { }
...and second made from Test.cpp
:
// ... code expanded from including "StdAfx.h"
extern "C" { void Foo(); }
int _tmain(int argc, _TCHAR* argv[])
{
Foo();
return 0;
}
In the second TU, the definition of Foo
is missing.
Why don't you simply put the definition of Foo
into the header file? Compiler cannot inline its code if it does not see it.