Tuesday, March 9, 2010

Link Error LNK2001 __cdecl

You may have ran into a link error like this while trying to link your driver or something.

Linking Executable - s:\depot.obj.amd64\proj\objchk\amd64\proj.dll
errors in directory s:\depot\proj
s:\depot\proj\proj.obj : error LNK2001: unresolved external symbol "long __cdecl _LibFunction(unsigned long,void *, ...)" (?_LibFunction@@YAJKPEAX000PEAPEAUHPNPCTX__@@@Z)

This recently happened to me while I was trying to link a C lib file into my C++ project. If this doesn't already cue you to the problem, this link error is caused by trying to link a C lib object in my C++ code. In the lib's header or around the #include in your C++ project, wrap the prototypes with this:

#if __cplusplus
extern "C" {
#endif

_LibFunction(...);
...

#if __cplusplus
}
#endif

If you just forgot to add the lib to your TARGETLIBS macro in your sources file, the error would look like this instead:

Linking Executable - s:\depot.obj.amd64chk\proj\objchk\amd64\proj.dll
errors in directory s:\depot\proj
s:\depot\proj.obj : error LNK2001: unresolved external symbol _LibFunction