Normally this refers to C++ naming conventions, but it applies to a number of non-C++ cases as well. C++ by default will use the name of a function, its parameters and its return type when calculating a name for a function. The linker function name for
void CALLTYPE test(void)
might come to anything in the table below depending on what CALLTYPE is and what language you are using. CALLTYPEs such as __cdecl, __fastcall, and __stdcall can change the naming conventions for a function/variable.
Calling convention | extern “C” or .C file | .CPP , .CXX or /TP |
C naming convention (__cdecl) | _test | ?test@@ZAXXZ |
Fastcall naming convention (__fastcall) | @test@0 | ?test@@YIXXZ |
Standard Call naming convention (__stdcall) | _test@0 | ?test@@YGXXZ |
Use extern “C” when calling a C function from a C++ program. Extern “C” forces use of the C naming convention for non-class C++ functions. Be aware of compiler switches like /TP or /Tc that force a file to be compiled as a C (/Tc) or C++ (/TP) file no matter what the filename extension, or you may get different function names than you expect.
Having function prototypes that have mismatched parameters can also cause this error. Name decoration incorporates the parameters of a function into the final decorated function name. Calling a function with the parameter types that do not match those in the function declaration may also cause LNK2001.
There is currently no standard for C++ naming between compiler vendors or even between different versions of a compiler. Therefore linking object files compiled with other compilers may not produce the same naming scheme and thus causes unresolved externals.